Local at-rules: keyframes and positionTry

Open in playground

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

reset();

// keyframes() writes the at-rule next to the styles that use it and returns its name; the rule goes into the sheet of the module.
const fade = css.keyframes({ from: css.opacity(0), to: css.opacity(1) });
const wiggle = css.keyframes({
  from: css.transform("rotate(0deg)"),
  "50%": css.cn(css.transform("rotate(8deg)"), css.opacity(0.6)),
  to: css.transform("rotate(0deg)"),
});
const spin = css.keyframes({ to: css.transform("rotate(360deg)") });

const toast = css.cn(
  css.p(4),
  css.bg("#0f172a"),
  css.color("white"),
  css.borderRadius("10px"),
  css.animation(fade, "400ms", "ease-out"),
);
const bell = css.cn(
  css.display("inline-block"),
  css.fontSize("2rem"),
  css.hover(css.animation(wiggle, "600ms", "ease-in-out", "infinite")),
);
const spinner = css.cn(
  css.width("2rem"),
  css.height("2rem"),
  css.border("4px solid #cbd5e1"),
  css.borderTopColor("#2563eb"),
  css.borderRadius("50%"),
  css.animation(spin, "900ms", "linear", "infinite"),
);

export default function App() {
  return (
    <div
      className={css.cn(css.flex().direction("column").alignItems("flex-start").gap(4), css.p(4))}
    >
      <div className={toast}>Saved. This fades in.</div>
      <span className={bell}>đŸ””</span>
      <div className={spinner} aria-label="Loading" />
    </div>
  );
}

keyframes() and positionTry() define an at-rule next to the styles that use it and return its name, a typed value that fits where the grammar wants a name.

import {
  const animation: Prop<"animation">
`<single-animation>#`. Chrome 43, Edge 12, Firefox 16, Safari 9, iOS 9, Android 43. [MDN](https://developer.mozilla.org/docs/Web/CSS/animation)
animation
,
function cn(...styles: Style[]): 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
,
const hover: StateWrap
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
,
const keyframes: (steps: Steps) => Keyframes
`keyframes({ from: opacity(0), to: opacity(1) })`: a `@keyframes` rule named by a hash of its content, in the sheet of the modules that use the name. Each step is checked like a class; no conditions.
keyframes
,
const margin: Prop<"margin">
`<'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
,
const opacity: Prop<"opacity">
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
,
const positionTry: ((style: Style) => PositionTry) & Prop<"position-try", Style>
`positionTry(cn(top("anchor(bottom)"), margin(2)))`: a `@position-try` rule, in the sheet of the modules that use its name. Only inset, margin, sizing, self-alignment, `position-anchor` and `position-area` are allowed (a build error). `position-try` is also a property: with value parts (`positionTry("flip-block")`) it is that shorthand, as before.
positionTry
,
const positionTryFallbacks: Prop<"position-try-fallbacks">
`none | [ [<dashed-ident> || <try-tactic>] | <position-area> ]#`. Chrome 128, Edge 128, Firefox 147, Safari 26, iOS 26, Android 128. [MDN](https://developer.mozilla.org/docs/Web/CSS/position-try-fallbacks)
positionTryFallbacks
,
const top: Prop<"top">
`auto | <length-percentage> | <anchor()> | <anchor-size()>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/top)
top
,
const transform: Prop<"transform">
`none | <transform-list>`. Chrome 36, Edge 12, Firefox 16, Safari 9, iOS 9, Android 36. [MDN](https://developer.mozilla.org/docs/Web/CSS/transform)
transform
,
} from "cssints" with { type: "cssints" }; export const const fade: Keyframesfade = function keyframes(steps: Steps): Keyframes
`keyframes({ from: opacity(0), to: opacity(1) })`: a `@keyframes` rule named by a hash of its content, in the sheet of the modules that use the name. Each step is checked like a class; no conditions.
keyframes
({ Steps.from?: Style | undefinedfrom: opacity<readonly [0]>(parts_0: 0): 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), Steps.to?: Style | undefinedto: opacity<readonly [1]>(parts_0: 1): 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
(1) });
export const const wiggle: Keyframeswiggle = function keyframes(steps: Steps): Keyframes
`keyframes({ from: opacity(0), to: opacity(1) })`: a `@keyframes` rule named by a hash of its content, in the sheet of the modules that use the name. Each step is checked like a class; no conditions.
keyframes
({
Steps.from?: Style | undefinedfrom: transform<readonly ["rotate(0deg)"]>(parts_0: "rotate(0deg)"): Style
`none | <transform-list>`. Chrome 36, Edge 12, Firefox 16, Safari 9, iOS 9, Android 36. [MDN](https://developer.mozilla.org/docs/Web/CSS/transform)
transform
("rotate(0deg)"),
"50%": function cn(...styles: Style[]): 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
(transform<readonly ["rotate(8deg)"]>(parts_0: "rotate(8deg)"): Style
`none | <transform-list>`. Chrome 36, Edge 12, Firefox 16, Safari 9, iOS 9, Android 36. [MDN](https://developer.mozilla.org/docs/Web/CSS/transform)
transform
("rotate(8deg)"), opacity<readonly [0.5]>(parts_0: 0.5): 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.5)),
Steps.to?: Style | undefinedto: transform<readonly ["rotate(0deg)"]>(parts_0: "rotate(0deg)"): Style
`none | <transform-list>`. Chrome 36, Edge 12, Firefox 16, Safari 9, iOS 9, Android 36. [MDN](https://developer.mozilla.org/docs/Web/CSS/transform)
transform
("rotate(0deg)"),
}); export const const above: PositionTryabove = function positionTry(style: Style): PositionTry (+1 overload)
`positionTry(cn(top("anchor(bottom)"), margin(2)))`: a `@position-try` rule, in the sheet of the modules that use its name. Only inset, margin, sizing, self-alignment, `position-anchor` and `position-area` are allowed (a build error). `position-try` is also a property: with value parts (`positionTry("flip-block")`) it is that shorthand, as before.
positionTry
(function cn(...styles: Style[]): 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
(top<readonly ["anchor(bottom)"]>(parts_0: "anchor(bottom)"): Style
`auto | <length-percentage> | <anchor()> | <anchor-size()>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/top)
top
("anchor(bottom)"), margin<readonly [2]>(parts_0: 2): 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
(2)));
export const const a: Stylea = animation<readonly [Keyframes, "200ms", "ease-out"]>(parts_0: Keyframes, parts_1: "200ms", parts_2: "ease-out"): Style
`<single-animation>#`. Chrome 43, Edge 12, Firefox 16, Safari 9, iOS 9, Android 43. [MDN](https://developer.mozilla.org/docs/Web/CSS/animation)
animation
(const fade: Keyframesfade, "200ms", "ease-out");
export const const b: Styleb = function hover(...styles: Style[]): Style
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
(animation<readonly [Keyframes, "1s", "infinite"]>(parts_0: Keyframes, parts_1: "1s", parts_2: "infinite"): Style
`<single-animation>#`. Chrome 43, Edge 12, Firefox 16, Safari 9, iOS 9, Android 43. [MDN](https://developer.mozilla.org/docs/Web/CSS/animation)
animation
(const wiggle: Keyframeswiggle, "1s", "infinite"));
export const const c: Stylec = positionTryFallbacks<readonly [PositionTry, "flip-inline"]>(parts_0: PositionTry, parts_1: "flip-inline"): Style
`none | [ [<dashed-ident> || <try-tactic>] | <position-area> ]#`. Chrome 128, Edge 128, Firefox 147, Safari 26, iOS 26, Android 128. [MDN](https://developer.mozilla.org/docs/Web/CSS/position-try-fallbacks)
positionTryFallbacks
(const above: PositionTryabove, "flip-inline");
export const const d: Keyframesd = function keyframes(steps: Steps): Keyframes
`keyframes({ from: opacity(0), to: opacity(1) })`: a `@keyframes` rule named by a hash of its content, in the sheet of the modules that use the name. Each step is checked like a class; no conditions.
keyframes
({ Steps.from?: Style | undefinedfrom: opacity<readonly ["lots"]>(parts_0: "opacity: unexpected \"lots\" in \"lots\", grammar <opacity-value>"): 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
("lots") }); // error: opacity: unexpected "lots"
Argument of type '"lots"' is not assignable to parameter of type '"opacity: unexpected \"lots\" in \"lots\", grammar <opacity-value>"'.
export const const e: Keyframese = function keyframes(steps: Steps): Keyframes
`keyframes({ from: opacity(0), to: opacity(1) })`: a `@keyframes` rule named by a hash of its content, in the sheet of the modules that use the name. Each step is checked like a class; no conditions.
keyframes
({ halfway: opacity<readonly [0]>(parts_0: 0): 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) }); // error: a step is from, to or a percentage
Object literal may only specify known properties, and 'halfway' does not exist in type 'Steps'.
export const const f: Stylef = positionTryFallbacks<readonly [Keyframes]>(parts_0: "position-try-fallbacks: unexpected \"⟨custom-ident⟩\" in \"⟨custom-ident⟩\", grammar none | [ [<dashed-ident> || <try-tactic~w>] | <position-area~w> ]#"): Style
`none | [ [<dashed-ident> || <try-tactic>] | <position-area> ]#`. Chrome 128, Edge 128, Firefox 147, Safari 26, iOS 26, Android 128. [MDN](https://developer.mozilla.org/docs/Web/CSS/position-try-fallbacks)
positionTryFallbacks
(fade); // error: a keyframes name is not a dashed ident
Argument of type 'Keyframes' is not assignable to parameter of type '"position-try-fallbacks: unexpected \"⟨custom-ident⟩\" in \"⟨custom-ident⟩\", grammar none | [ [<dashed-ident> || <try-tactic~w>] | <position-area~w> ]#"'.
@layer _.k {
  @keyframes kf-0e6af964 {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  @position-try --pt-23965b44 {
    top: anchor(bottom);
    margin: 0.5rem;
  }
}
@layer _.a {
  ._44ecsn {
    animation: kf-0e6af964 200ms ease-out;
  }
}