Global rules, attributes and parent states

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import * as css from "cssints" with { type: "cssints" };

import "./base.ts" with { type: "cssints" };

reset();

// attr() styles an attribute state; group() marks an ancestor and within() reads its state.
const nav = css.cn(css.flex().direction("column").gap(1), css.p(3), css.maxWidth("22rem"));
const row = css.cn(
  css.group(),
  css.flex().alignItems("center").justifyContent("space-between"),
  css.padding("0.5rem", "0.75rem"),
  css.borderRadius("8px"),
  css.hover(css.cn(css.bg("#f1f5f9"), css.color("#0f172a"))),
  css.attr(
    "aria-current",
    "page",
  )(css.cn(css.color("#1d4ed8"), css.fontWeight(700), css.bg("#dbeafe"))),
);
const actions = css.cn(
  css.opacity(0),
  css.fontSize("0.8125rem"),
  css.within(css.hover)(css.opacity(1)),
  css.within(css.focusVisible)(css.opacity(1)),
);
const pdf = css.attr("href", ".pdf", "$=")(css.textDecoration("underline"));

export default function App() {
  return (
    <nav className={nav} aria-label="Files">
      {[
        ["Overview", "overview.html", true],
        ["Pricing", "pricing.html", false],
        ["Handbook", "handbook.pdf", false],
      ].map(([name, href, current]) => (
        <div className={row} aria-current={current ? "page" : undefined}>
          <a href={href as string} className={css.cn(css.color("inherit"), pdf)}>
            {name}
          </a>
          <span className={actions}>Rename · Delete</span>
        </div>
      ))}
    </nav>
  );
}
import * as css from "cssints" with { type: "cssints" };

// Rules with no class: a reset, the colour scheme. They go to layer _.k, below every class, so a component never fights them.
css.global("*, *::before, *::after", css.boxSizing("border-box"));
css.global("body", css.margin("0"), css.fontFamily("system-ui, sans-serif"));
css.global(
  ":root",
  css.colorScheme("light"),
  css.media("(prefers-color-scheme: dark)")(css.colorScheme("dark")),
);
import * as import csscss from "cssints" with { type: "cssints" };

const const dark: css.QueryWrapdark = import csscss.
const media: Condition
<"(prefers-color-scheme: dark)">(query: "(prefers-color-scheme: dark)") => css.QueryWrap (+2 overloads)
media
("(prefers-color-scheme: dark)");
// a reset: rules with no class import csscss.const global: <"*, *::before, *::after">(selector: "*, *::before, *::after", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("*, *::before, *::after", import csscss.const boxSizing: <readonly ["border-box"]>(parts_0: "border-box") => css.Style
`content-box | border-box`. Chrome 10, Edge 12, Firefox 29, Safari 5.1, iOS 6, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/box-sizing)
boxSizing
("border-box"));
import csscss.const global: <"body">(selector: "body", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("body", import csscss.const margin: <readonly ["0"]>(parts_0: "0") => css.Style
`<'margin-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/margin)
margin
("0"), import csscss.const margin: <readonly ["8px"]>(parts_0: "8px") => css.Style
`<'margin-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/margin)
margin
("8px")); // the later margin wins
import csscss.const global: <":root">(selector: ":root", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
(":root", import csscss.const colorScheme: <readonly ["light"]>(parts_0: "light") => css.Style
`normal | [ light | dark | <custom-ident> ]+ && only?`. Chrome 81, Edge 81, Firefox 96, Safari 13, iOS 13, Android 81. [MDN](https://developer.mozilla.org/docs/Web/CSS/color-scheme)
colorScheme
("light"), const dark: (...styles: css.Style[]) => css.Styledark(import csscss.const colorScheme: <readonly ["dark"]>(parts_0: "dark") => css.Style
`normal | [ light | dark | <custom-ident> ]+ && only?`. Chrome 81, Edge 81, Firefox 96, Safari 13, iOS 13, Android 81. [MDN](https://developer.mozilla.org/docs/Web/CSS/color-scheme)
colorScheme
("dark")));
import csscss.const global: <"a[href^='http'] > b">(selector: "a[href^='http'] > b", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("a[href^='http'] > b", import csscss.const color: <readonly ["red"]>(parts_0: "red") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("red"));
import csscss.const global: <"a[href">(selector: "global(): unbalanced brackets or quotes in \"a[href\"", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("a[href", import csscss.const color: <readonly ["red"]>(parts_0: "red") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("red")); // error: a bracket is open
Argument of type '"a[href"' is not assignable to parameter of type '"global(): unbalanced brackets or quotes in \"a[href\""'.
import csscss.const global: <"a >">(selector: "a >", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("a >", import csscss.const color: <readonly ["red"]>(parts_0: "red") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("red")); // selector: unexpected end in "a >", expected <complex-selector-list>
import csscss.const global: <"a">(selector: "a", ...styles: css.Style[]) => void
`global(":root", media(dark)(colorScheme("dark")))`: a rule with no class, in layer `_.k`, in the sheet of the module that calls it. The selector is a `<complex-selector-list>` (the build reads it, the types check the brackets and quotes); the styles are plain declarations, under media(), container() or supports() if you like.
global
("a", import csscss.const hover: (...styles: css.Style[]) => css.Style
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
(import csscss.const color: <readonly ["red"]>(parts_0: "red") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("red"))); // global() takes media(), container() and supports() conditions, not :hover: write the state in the selector
// an attribute state, and the state of a marked ancestor export const const row: css.Stylerow = import csscss.function cn(...styles: css.Style[]): css.Style (+1 overload)
Merges styles into one class string. What is known at build time merges (the later property wins); a runtime value (`on && p(4)`, `props.class`, a state call) is joined when the code runs, and a style in it replaces the classes it overrides while it is active.
cn
(import csscss.const group: <never>(name?: undefined) => css.Style
`group()` is the marker class `_g` for the element whose state `within()` reads; `group("row")` is `_g-row`, for a nested group. It has no CSS of its own.
group
(), import csscss.const p: <readonly [2]>(parts_0: 2) => css.Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(2));
export const const link: css.Stylelink = import csscss.function cn(...styles: css.Style[]): css.Style (+1 overload)
Merges styles into one class string. What is known at build time merges (the later property wins); a runtime value (`on && p(4)`, `props.class`, a state call) is joined when the code runs, and a style in it replaces the classes it overrides while it is active.
cn
(
import csscss.const color: <readonly ["gray"]>(parts_0: "gray") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("gray"),
import csscss.const attr: <"aria-current">(name: "aria-current", value: string, op?: css.AttrOp) => css.StateWrap (+1 overload)attr("aria-current", "page")(import csscss.const color: <readonly ["blue"]>(parts_0: "blue") => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
("blue")),
import csscss.const within: <never>(state: css.StateWrap, name?: undefined) => css.Wrap
`within(hover)(color("red"))` is the style while a marked ancestor is hovered: `:where(._g:hover) ._x`, one class of specificity, one condition. `within(hover, "row")` reads the `group("row")` ancestor.
within
(import csscss.const hover: css.StateWrap
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
)(import csscss.const textDecoration: <readonly ["underline"]>(parts_0: "underline") => css.Style
`<'text-decoration-line'> || <'text-decoration-style'> || <'text-decoration-color'> || <'text-decoration-thickness'>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/text-decoration)
textDecoration
("underline")),
); export const const icon: css.Styleicon = import csscss.const attr: <"href">(name: "href", value: string, op?: css.AttrOp) => css.StateWrap (+1 overload)attr("href", ".pdf", "$=")(import csscss.const opacity: <readonly [0.8]>(parts_0: 0.8) => css.Style
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
(0.8));
export const const nested: css.Stylenested = import csscss.function cn(...styles: css.Style[]): css.Style (+1 overload)
Merges styles into one class string. What is known at build time merges (the later property wins); a runtime value (`on && p(4)`, `props.class`, a state call) is joined when the code runs, and a style in it replaces the classes it overrides while it is active.
cn
(
import csscss.const group: <"card">(name?: "card" | undefined) => css.Style
`group()` is the marker class `_g` for the element whose state `within()` reads; `group("row")` is `_g-row`, for a nested group. It has no CSS of its own.
group
("card"),
import csscss.const within: <"card">(state: css.StateWrap, name?: "card" | undefined) => css.Wrap
`within(hover)(color("red"))` is the style while a marked ancestor is hovered: `:where(._g:hover) ._x`, one class of specificity, one condition. `within(hover, "row")` reads the `group("row")` ancestor.
within
(import csscss.const focusVisible: css.StateWrap
Chrome 86, Edge 86, Firefox 85, Safari 15.4, iOS 15.4, Android 86. [MDN](https://developer.mozilla.org/docs/Web/CSS/:focus-visible)
focusVisible
, "card")(import csscss.const outline: <readonly ["2px solid"]>(parts_0: "2px solid") => css.Style
`<'outline-width'> || <'outline-style'> || <'outline-color'>`. Chrome 94, Edge 94, Firefox 88, Safari 16.4, iOS 16.4, Android 94. [MDN](https://developer.mozilla.org/docs/Web/CSS/outline)
outline
("2px solid")),
); import csscss.const attr: <"a b">(name: "\"a b\" is not a name") => css.StateWrap (+1 overload)attr("a b"); // error: a name has no space
Argument of type '"a b"' is not assignable to parameter of type '"\"a b\" is not a name"'.
import csscss.const attr: <"a">(name: "a", value: string, op?: css.AttrOp) => css.StateWrap (+1 overload)attr("a", "x", "!="); // error: not an operator
Argument of type '"!="' is not assignable to parameter of type 'AttrOp | undefined'.
import csscss.const within: <never>(state: css.StateWrap, name?: undefined) => css.Wrap
`within(hover)(color("red"))` is the style while a marked ancestor is hovered: `:where(._g:hover) ._x`, one class of specificity, one condition. `within(hover, "row")` reads the `group("row")` ancestor.
within
(css.media("(width >= 40rem)")); // error: a media query is not a state
Argument of type 'QueryWrap' is not assignable to parameter of type 'StateWrap'. Type 'QueryWrap' is not assignable to type '{ readonly __state: true; }'.
@layer _.k {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }
  body {
    margin: 8px;
  }
  :root {
    color-scheme: light;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      color-scheme: dark;
    }
  }
  a[href^="http"] > b {
    color: red;
  }
}
@layer _.b {
  :where(._g:hover) ._72037 {
    text-decoration: underline;
  }
  ._fc1pxh[aria-current="page"] {
    color: blue;
  }
  ._pl3vkk[href$=".pdf"] {
    opacity: 0.8;
  }
}