import React from "react"
import expectRender from "../expect-to-same-render"
import classNaming, { classBeming } from "../src"
import type { ClassNamed, Undefineds } from "../src"
import type {ClassHash, ClassNamesProperty} from "../src"
// import css_module from "./button.module.css"
const css_module = {button: "BTN"}
it("Basic usage", () => {
type Props = {
isValid: boolean
readOnly: boolean
}
// isValid = false, readOnly = false
function FormButtons({isValid, readOnly}: Props) {
const cssClasses = classNaming()
const buttonClass = cssClasses({"button": true}) // "button"
return <>
{/* className="button_submit button button--disabled" */}
>
}
expectRender(
).toSame(<>
>)
})
it("Strict type", () => {
type Props = {readOnly?: boolean}
const {readOnly} = {} as Props
const cssClasses = classNaming()
const disabling = cssClasses({
//@ts-expect-error Type 'boolean | undefined' is not assignable to type 'boolean'
"button--disabled": readOnly
})
expect({...disabling}).toStrictEqual({className: "button--disabled"})
})
it("Single source of truth", () => {
const cssClasses = classNaming()
const isValidClass = cssClasses({"button--disabled": /* !isValid */ false })
// ... more code
const buttonClass = isValidClass({button: true})
// ... more code
const disablingClass = buttonClass({
//@ts-expect-error Type 'boolean' is not assignable to type 'never'
"button--disabled": true
})
expect({...disablingClass}).toStrictEqual({
className: "button button--disabled"
})
})
it("Declare own component's CSS classes", () => {
type MyClassNames = ClassNamesProperty<{
button: ClassHash
button_submit: ClassHash
"button--disabled": ClassHash
}>
type Props = {
isValid: boolean
readOnly: boolean
}
// isValid = false, readOnly = false
function FormButtons({isValid, readOnly}: Props) {
const cssClasses = classNaming()
const buttonClass = cssClasses({button: true})
return <>
>
}
expectRender(
).toSame(<>
>)
})
it("Using ClassHash", () => {
// CSS-module
const { button } = css_module
// Module simulation
type CssModuleSimulation = { button_submit: ClassHash }
const { button_submit } = {} as CssModuleSimulation
type MyClassNames = ClassNamesProperty<
typeof css_module &
CssModuleSimulation &
{
"button--disabled": ClassHash
}
>
type Props = {
isValid: boolean
readOnly: boolean
}
// isValid = false, readOnly = false
function FormButtons({isValid, readOnly}: Props) {
const cssClasses = classNaming()
const buttonClass = cssClasses({ button })
return <>
>
}
expectRender(
).toSame(<>
>)
})
it("bem leaf", () => {
type Props = ClassNamesProperty
& { focused?: boolean }
function DialogButton({focused}: Props) {
const bem = classBeming()
return
}
const props = {focused: true} as Props
expectRender(
).toSame(
)
})
describe("bem from https://material.io/components/buttons/web#contained-button", () => {
const CONSTS = {ripple: "ripple-upgraded", icon: {"material-icons": true}} as const
type Props = ClassNamed & ClassNamesProperty
& { focused?: boolean; clicking?: boolean }
const {ripple, icon} = CONSTS
const {
button__icon,
button__label,
button__ripple
} = {} as Undefineds
function Button(props: Props) {
const {
clicking,
focused = false,
} = props
const bem = classBeming(props)
return
}
expectRender(
).toSame(
)
})
type MaterialClasses = {
"material-icons": ClassHash
ripple: ClassHash
"ripple--bounded": ClassHash
"ripple--unbounded": ClassHash
"ripple--background-focused": ClassHash
"ripple--foreground-activation": ClassHash
"ripple--foreground-deactivation": ClassHash
"ripple-upgraded": ClassHash
"ripple-upgraded--bounded": ClassHash
"ripple-upgraded--unbounded": ClassHash
"ripple-upgraded--background-focused": ClassHash
"ripple-upgraded--foreground-activation": ClassHash
"ripple-upgraded--foreground-deactivation": ClassHash
button: ClassHash
"button--raised": ClassHash
"button--type--raised": ClassHash
"button--type--outlined": ClassHash
button__label: ClassHash
button__ripple: ClassHash
button__icon: ClassHash
dialog: ClassHash
dialog__button: ClassHash
}