Rule Order & Important
Rule Order
Styles are applied in the following order, with #1 being styles that will have the highest priority:
- class selector (eg. media queries) values with
important()
- class values with
important()
style
prop- class selector (eg. media queries) values
- class values
- cascading styles (eg.
color
)
Important
Just as in CSS if you need a value to override other rules that have higher precedence you can wrap those values in an important()
:
import {important} from "style-composer";
export const $Card = composeClass("card", () => ({
backgroundColor: important("red"),
[media({maxWidth: 700})]: {
// even though usually a selector will override the parent rules,
// because the parent rule is marked as important it will not be overriden
backgroundColor: "blue"
}
}));
Demo
function Button({classes, children}) {
// NOTE: DO NOT define classes inside a component, this is merely for demonstration
const $Button = composeClass("button", () => ({
padding: 10,
borderRadius: 30,
textAlign: "center",
width: 150,
margin: 5,
[disabled()]: {
// try taking away the important() call
backgroundColor: important("#aaa"),
}
}));
const [isDisabled, setDisabled] = useState(true);
return <StyledView style={{color: "white"}}>
<StyledView style={{display: "flex", flexDirection: "row"}}>
<StyledText>Button disabled</StyledText>
<CheckBox
value={isDisabled}
onValueChange={setDisabled}/>
</StyledView>
<StyledTouchableOpacity
classes={[$Button, classes]}
style={{backgroundColor: "#673ab7"}}
pseudoClasses={[isDisabled && disabled]}
activeOpacity={isDisabled ? 1 : 0.5}
>
<StyledText>Button</StyledText>
</StyledTouchableOpacity>
</StyledView>;
}