A simple library for typesafe CSS.
"All" (hopefully) built-in CSS properties are supported. A proxy is used, so any property will work at runtime, even if it raises a TypeScript error.
There are also some convenience aliases for common CSS patterns, such as "pad", "hbox", "vbox", and "wrap". Some aliases are parameterless, such as "absolute".
Go to definition to see the underlying alias or CSS property.
import { css } from "typesafecss";
preact.render(
document.getElementById("main"),
<div className={
css.pad(2).margin(2)
.absolute.pos(0, 0).zIndex(1)
.hsl(0, 0, 25)
.vbox(2).alignItems("stretch")
.overflow("auto")
.maxHeight("50vh")
.border("1px solid hsl(0, 0%, 10%)")
}>
</div>
)
import { css } from "typesafecss";
return <div className={css.hsl(0, 0, 50) + (this.props.bold && css.fontWeight("bold"))} />
"soft" as a second parameter will inject a CSS rule with a lower precedence than the default rule. This allows generic components to have a default style that can be overridden by the parent component.
import { css } from "typesafecss";
return <div className={this.props.inputClassName + css.alignItems("center", "soft")} />
import { css } from "typesafecss";
return <div className={css.background("hsl(0, 0%, 50%)", "hover")} />
import { css } from "typesafecss";
return <div className={css.background("hsl(0, 0%, 50%)", "active")} />
import { css } from "typesafecss";
return <div className={css.background("hsl(0, 0%, 50%)", "focus")} />
import { css } from "typesafecss";
// { background: hsl(0, 0%, 50%) !important; }
return <div className={css.background("hsl(0, 0%, 50%)", "important")} />
import { css } from "typesafecss";
// :focus { background: hsl(0, 0%, 50%); }
return <div className={css.background("hsl(0, 0%, 50%)", "focus", "soft")} />
import { css } from "typesafecss";
// $ELEMENT_ID$:hover:active:focus { background: hsl(0, 0%, 50%) !important; }
return <div className={css.background("hsl(0, 0%, 50%)", "hover", "active", "focus", "important")} />
All hover states may also be triggered by a parent class called "trigger-hover"
import { css } from "typesafecss";
return <div className={"trigger-hover"}>
text here
<div className={css.opacity(0.5).opacity(1, "hover")}>
more text here
</div>
</div>