|
| 1 | +import hyperdiv as hd |
| 2 | +from ...router import router |
| 3 | +from ...page import page |
| 4 | +from ...code_examples import docs_markdown |
| 5 | + |
| 6 | + |
| 7 | +@router.route("/extending-hyperdiv/built-in-components") |
| 8 | +def built_in_components(): |
| 9 | + with page() as p: |
| 10 | + p.title("# Extending Built-In Components") |
| 11 | + |
| 12 | + docs_markdown( |
| 13 | + """ |
| 14 | +
|
| 15 | + Hyperdiv built-in components can be extended with new |
| 16 | + style props. Also, if a built-in style prop doesn't expose |
| 17 | + CSS attributes that you need, you can override/replace |
| 18 | + that prop with a prop that does. |
| 19 | +
|
| 20 | + Hyperdiv's prop and type system enables defining props |
| 21 | + that are compiled to CSS and applied to the component in |
| 22 | + the browser. For example, we can create a new component |
| 23 | + type that extends @component(box) with an |
| 24 | + [opacity](https://developer.mozilla.org/en-US/docs/Web/CSS/opacity) |
| 25 | + prop: |
| 26 | +
|
| 27 | + ```py |
| 28 | + class opacity_box(hd.box): |
| 29 | + opacity = hd.Prop( |
| 30 | + hd.CSSField( |
| 31 | + "opacity", |
| 32 | + hd.Optional(hd.ClampedFloat(0, 1)) |
| 33 | + ), |
| 34 | + None |
| 35 | + ) |
| 36 | +
|
| 37 | + with opacity_box( |
| 38 | + opacity=0.3, |
| 39 | + border="1px solid green", |
| 40 | + padding=1, |
| 41 | + gap=1, |
| 42 | + ) as box: |
| 43 | + hd.text("A low-opacity box.") |
| 44 | + hd.button("A button") |
| 45 | +
|
| 46 | + ``` |
| 47 | +
|
| 48 | + Hyperdiv provides a type, @prop_type(CSSField), that |
| 49 | + enables defining new CSS attributes. In the example above, |
| 50 | + we define a new prop, `opacity`. Its type, |
| 51 | + `CSSField("opacity", hd.Optional(hd.ClampedFloat(0, 1)))`, |
| 52 | + specifies that |
| 53 | +
|
| 54 | + 1. this prop will be compiled to CSS |
| 55 | + 2. the CSS attribute name is `"opacity"` |
| 56 | + 3. the allowed values are floats between 0 and 1. The |
| 57 | + value of the prop will be rendered directly in the CSS |
| 58 | + `opacity` field. |
| 59 | +
|
| 60 | + The CSS `opacity: 0.3;` will be added to instances of this |
| 61 | + component. |
| 62 | +
|
| 63 | + CSS props work like normal Hyperdiv props and can be read |
| 64 | + and mutated by Python code. For example, we can control |
| 65 | + the box opacity with a slider: |
| 66 | +
|
| 67 | + ```py |
| 68 | + class opacity_box(hd.box): |
| 69 | + opacity = hd.Prop( |
| 70 | + hd.CSSField( |
| 71 | + "opacity", |
| 72 | + hd.Optional(hd.ClampedFloat(0, 1)) |
| 73 | + ), |
| 74 | + None |
| 75 | + ) |
| 76 | +
|
| 77 | + opacity = hd.slider( |
| 78 | + min_value=0, |
| 79 | + max_value=1, |
| 80 | + value=1, |
| 81 | + step=0.01 |
| 82 | + ) |
| 83 | +
|
| 84 | + with opacity_box( |
| 85 | + opacity=opacity.value, |
| 86 | + border="1px solid green", |
| 87 | + padding=1, |
| 88 | + gap=1, |
| 89 | + ) as box: |
| 90 | + hd.text("Drag the slider to change the opacity.") |
| 91 | + hd.button("A button") |
| 92 | +
|
| 93 | + ``` |
| 94 | +
|
| 95 | + Note that when the value of a CSS prop is `None`, its |
| 96 | + corresponding CSS field will not be sent to the browser at |
| 97 | + all. `None` corresponds to default browser behavior. |
| 98 | +
|
| 99 | + The props defined by @component(Styled), from which most |
| 100 | + Hyperdiv components inherit, are defined in this way. |
| 101 | +
|
| 102 | + """ |
| 103 | + ) |
0 commit comments