サンプル
実践的なサンプル。ライブデモ。
サンプルを選ぶと全画面で開き、ライブプレビューとソースが左右に並んで表示されます。
サンプルを選ぶと全画面で開き、ライブプレビューとソースが左右に並んで表示されます。
class は css() で作り、ちょっとした条件分岐は cx() に任せます。
css() creates the base class. cx() adds the selected class only when state changes.
import 'nuclo' const { css, cx } = createCss({ colors: { primary: "#ff3f00", border: "#e5e7eb", text: "#1f2937", }, }) const baseButton = css({ px: 14, py: 9, rounded: 6, border: "1px solid", borderColor: "border", color: "text", cursor: "pointer", }) const selectedButton = css({ bg: "primary", color: "white", borderColor: "primary", }) let selected = false export function StyledButton() { return div( p( code("css()"), " creates the base class. ", code("cx()"), " adds the selected class only when state changes.", ), button( // cx() joins the classes that are truthy () => cx(baseButton, selected ? selectedButton : null), () => selected ? "Selected" : "Select", { onClick: () => { selected = !selected; update() } }, ), ) }