StyleHelper
StyleHelper is a utility tool designed for defining conditional values for component property styles. You can write complex conditional styles concisely based on one or more conditions, such as viewport size.
StyleHelper methods
| Method | Description | |
|---|---|---|
default | <T>(defaultValue: T) => ConditionalStyle<T, Conditions> | The default style value to be used when no other conditions are met. |
when | <T>(conditions: Conditions, value: T) => ConditionalStyle<T, Conditions> | Conditional rules for component styles. All conditions, represented in object form, must be satisfied to apply the corresponding value. The when method can be chained to construct more complex styles. |
Example
The following example demonstrates how the View component responsively adjusts its layout based on viewport breakpoints: when the viewport width is greater than small, the layout switches from a single column to a double column.
<Grid
rows={['1fr', 'auto']}
columns={StyleHelper.default(['1fr'])
.when(
{
viewportInlineSize: {
min: 'extraSmall',
},
},
['1fr'],
)
.when(
{
viewportInlineSize: {
min: 'small',
},
},
['1fr'],
)
.when(
{
viewportInlineSize: {
min: 'medium',
},
},
['1fr', '1fr'],
)
.when(
{
viewportInlineSize: {
min: 'large',
},
},
['1fr', '1fr'],
)}
spacing="loose"
padding="loose"
>
<View>View1</View>
<View>View2</View>
<View>View3</View>
<View>View4</View>
</Grid>
Type descriptions
Conditions
Conditional styling supports the following conditions.
Multiple conditions can be set within the same when method.
| Name | Type | Description |
|---|---|---|
viewportInlineSize | {min: "extraSmall" | "small" | "medium" | "large"} | Configure the viewport breakpoint value. When the device's viewport width falls within a certain breakpoint range, the condition is met. |
ConditionalStyle
| Name | Type | Description |
|---|---|---|
conditionals | ConditionalValue<T, AcceptedConditions>[] | An array of conditional values. |
default | T | Default value applied when none of the specified conditional values are met. |
ConditionalValue
| Name | Type | Description |
|---|---|---|
conditions | AcceptedConditions | Conditions that must be satisfied to apply the given value. At least one condition must be specified. |
value | T | The value to be applied when conditions are met. |
Viewport breakpoints
Viewport breakpoints are critical criteria for evaluating conditions. When the viewport width falls within the following ranges, the corresponding value will be triggered. For example, when the viewport width is 450px, StyleHelper will recognize it as the base breakpoint.
base:0px≤ viewport width <580pxextraSmall:580px≤ viewport width <750pxsmall:750px≤ viewport width <1000pxmedium:1000px≤ viewport width <1200pxlarge: viewport width ≥1200px