= C extends {
readonly [ConsumerProps]: infer P;
}
- ? Omit & { children?: ReactNode }
+ ? DistributiveOmit
& { children?: ReactNode }
: C extends ForwardRefExoticComponent
? Omit & {
className?: string | undefined;
diff --git a/packages/react/src/types/contract.ts b/packages/react/src/types/contract.ts
new file mode 100644
index 00000000..d8417065
--- /dev/null
+++ b/packages/react/src/types/contract.ts
@@ -0,0 +1,133 @@
+import type { ContractRuleEntry, VariantKeyOf } from '@animus-ui/core';
+import type {
+ ContractTagMatches,
+ ContractValues,
+ StripIndex,
+ VariantOptionMap,
+} from '@animus-ui/core/binding';
+
+type AttrValue = A extends keyof Native
+ ? Exclude
+ : string;
+
+type RuleAttrs = Rule extends {
+ readonly require: { readonly attrs: readonly (infer A extends string)[] };
+}
+ ? A
+ : never;
+
+type RuleAnyAttr = Rule extends {
+ readonly require: { readonly anyAttr: readonly (infer G extends string)[] };
+}
+ ? G
+ : never;
+
+type RuleStates = Rule extends {
+ readonly dischargedByState: readonly (infer S extends string)[];
+}
+ ? S
+ : never;
+
+type RequireAll = [A] extends [never]
+ ? unknown
+ : { [K in A]: AttrValue };
+
+type RequireOne = [G] extends [never]
+ ? unknown
+ : G extends string
+ ? { [K in G]: AttrValue }
+ : never;
+
+type SetState = S extends string ? { [K in S]: true } : never;
+
+type RuleDischarge = [
+ RuleAttrs | RuleAnyAttr,
+] extends [never]
+ ? unknown
+ :
+ | (RequireAll, Native> &
+ RequireOne, Native>)
+ | SetState>;
+
+type RuleWhen = Rule extends { readonly when: infer When } ? When : {};
+
+type Expected = ContractValues<
+ Exclude
+>;
+
+type ConditionKeys = {
+ [K in keyof When]-?: [Expected] extends [never] ? never : K;
+}[keyof When] &
+ string;
+
+type DefaultOf = K extends keyof StripIndex
+ ? StripIndex[K] extends { defaultVariant?: infer D }
+ ? VariantKeyOf>
+ : never
+ : never;
+
+type DefaultMatches = [
+ Extract, Expected>,
+] extends [never]
+ ? false
+ : true;
+
+type Matching = {
+ [K in Keys as DefaultMatches extends true ? never : K]: Expected<
+ When,
+ K
+ >;
+} & {
+ [K in Keys as DefaultMatches extends true ? K : never]?:
+ | Expected
+ | undefined;
+};
+
+type Unmatched =
+ DefaultMatches extends true
+ ? {
+ [P in K]: Exclude<
+ VariantOptionMap[P & keyof VariantOptionMap],
+ Expected
+ >;
+ }
+ : {
+ [P in K]?:
+ | Exclude<
+ VariantOptionMap[P & keyof VariantOptionMap],
+ Expected
+ >
+ | undefined;
+ };
+
+type Conditioned = [ConditionKeys] extends [never]
+ ? Discharge
+ :
+ | (Matching> & Discharge)
+ | {
+ [K in ConditionKeys]: Unmatched;
+ }[ConditionKeys];
+
+type RuleProps =
+ ContractTagMatches<
+ Tag,
+ Rule extends { readonly whenTag: infer WhenTag } ? WhenTag : undefined
+ > extends true
+ ? RuleDischarge extends infer Discharge
+ ? unknown extends Discharge
+ ? unknown
+ : Conditioned, Discharge>
+ : never
+ : unknown;
+
+export type ContractProps<
+ C extends readonly ContractRuleEntry[],
+ Tag extends string,
+ V,
+ Native,
+> = C extends readonly [
+ infer Rule,
+ ...infer Rest extends readonly ContractRuleEntry[],
+]
+ ? RuleProps & ContractProps
+ : unknown;
diff --git a/packages/react/types/published/contract.tsx b/packages/react/types/published/contract.tsx
new file mode 100644
index 00000000..66894620
--- /dev/null
+++ b/packages/react/types/published/contract.tsx
@@ -0,0 +1,108 @@
+import type { ComponentPropsWithRef } from 'react';
+
+import { ds } from './kit';
+
+import type { Assert, Equal, RequiredKeys } from './guards';
+
+const IconButton = ds
+ .styles({ display: 'inline-flex' })
+ .variant({
+ prop: 'kind',
+ defaultVariant: 'solid',
+ variants: { solid: { padding: '8px 12px' }, iconOnly: { padding: '8px' } },
+ })
+ .contract({
+ when: { kind: 'iconOnly' },
+ require: { anyAttr: ['aria-label', 'aria-labelledby'] },
+ })
+ .asElement('button');
+
+const Glyph = ds
+ .styles({ display: 'inline-flex' })
+ .variant({
+ prop: 'kind',
+ defaultVariant: 'iconOnly',
+ variants: { labelled: {}, iconOnly: {} },
+ })
+ .contract({
+ when: { kind: 'iconOnly' },
+ require: { attrs: ['aria-label'] },
+ })
+ .asElement('span');
+
+const Picture = ds
+ .styles({ display: 'block' })
+ .states({ decorative: { pointerEvents: 'none' } })
+ .contract({ require: { attrs: ['alt'] }, dischargedByState: ['decorative'] })
+ .asElement('img');
+
+const Link = ds
+ .styles({ color: 'ink' })
+ .contract({ whenTag: 'a', require: { attrs: ['href'] } })
+ .asElement('a');
+
+const ExtendedPicture = Picture.extend()
+ .styles({ opacity: 0.9 })
+ .asElement('img');
+
+const Plain = ds.styles({ display: 'flex' }).asElement('div');
+
+export const accepted = (
+ <>
+ Save
+ Save
+
+ ×
+
+
+ ×
+
+ Label
+ ★
+
+
+
+ Docs
+ Not a link
+ Plain
+ >
+);
+
+// @ts-expect-error
+export const iconOnlyUnlabelled = ×;
+export const iconOnlyUndefinedLabel = (
+ // @ts-expect-error
+
+ ×
+
+);
+// @ts-expect-error
+export const defaultIconOnly = ★;
+// @ts-expect-error
+export const explicitIconOnly = ★;
+// @ts-expect-error
+export const noAlt = ;
+// @ts-expect-error
+export const notDecorative = ;
+// @ts-expect-error
+export const extendedNoAlt = ;
+// @ts-expect-error
+export const anchorNoHref = Docs;
+
+export const ruleKey = ds
+ .styles({})
+ // @ts-expect-error
+ .contract({ requires: { attrs: ['href'] } });
+export const requirementKey = ds.styles({}).contract({
+ // @ts-expect-error
+ require: { attrs: ['href'], anyAttrs: ['title'] },
+});
+export const conditionAxis = ds
+ .styles({})
+ .variant({ prop: 'kind', variants: { solid: {} } })
+ // @ts-expect-error
+ .contract({ when: { tone: 'solid' } });
+
+export type _NoContract = Assert<
+ Equal>, never>
+>;
diff --git a/packages/showcase/src/components/docs/ChainStep.tsx b/packages/showcase/src/components/docs/ChainStep.tsx
index 28702627..ae57d360 100644
--- a/packages/showcase/src/components/docs/ChainStep.tsx
+++ b/packages/showcase/src/components/docs/ChainStep.tsx
@@ -655,7 +655,7 @@ const LAYER_KEYS = ['0', '1', '2', '3', '4', '5'] as const;
type LayerKey = (typeof LAYER_KEYS)[number];
function layerKeyAt(index: number): LayerKey {
- const key = LAYER_KEYS[index];
+ const key = LAYER_KEYS.at(index);
if (key === undefined) {
throw new Error(`ChainStep supports at most ${LAYER_KEYS.length} steps.`);
}
@@ -720,7 +720,7 @@ export function ChainStep({
return () => window.removeEventListener('keydown', handler);
}, [isControlled, controlledClick, activeStep, steps.length]);
- const step = activeStep >= 0 ? steps[activeStep] : null;
+ const step = activeStep >= 0 ? steps.at(activeStep) : null;
const layerKey = activeStep >= 0 ? layerKeyAt(activeStep) : LAYER_KEYS[0];
return (
diff --git a/packages/showcase/src/components/docs/PageNav.tsx b/packages/showcase/src/components/docs/PageNav.tsx
index 0bf15b50..2ab3c7c2 100644
--- a/packages/showcase/src/components/docs/PageNav.tsx
+++ b/packages/showcase/src/components/docs/PageNav.tsx
@@ -73,7 +73,7 @@ export function PageNav() {
if (idx === -1) return null;
const prev = idx > 0 ? ALL_PAGES[idx - 1] : null;
- const next = idx < ALL_PAGES.length - 1 ? ALL_PAGES[idx + 1] : null;
+ const next = idx < ALL_PAGES.length - 1 ? ALL_PAGES.at(idx + 1) : null;
if (!prev && !next) return null;
diff --git a/packages/showcase/src/components/surfaces/SyntaxBlock.tsx b/packages/showcase/src/components/surfaces/SyntaxBlock.tsx
index 4b666cbd..ba8b4a64 100644
--- a/packages/showcase/src/components/surfaces/SyntaxBlock.tsx
+++ b/packages/showcase/src/components/surfaces/SyntaxBlock.tsx
@@ -303,15 +303,9 @@ type HighlightLanguage =
| 'javascript'
| 'plain';
-const HIGHLIGHT_LANGUAGE = {
- tsx: 'tsx',
- css: 'css',
- jsx: 'jsx',
- typescript: 'typescript',
- javascript: 'javascript',
- sh: 'plain',
- svelte: 'plain',
-} as const satisfies Record;
+function highlightLanguage(lang: Language): HighlightLanguage {
+ return lang === 'sh' || lang === 'svelte' ? 'plain' : lang;
+}
function detectLanguage(code: string): Language {
if (
@@ -399,7 +393,7 @@ export function SyntaxBlock({
{({ tokens: tokenLines, getLineProps, getTokenProps }) => (
@@ -407,32 +401,27 @@ export function SyntaxBlock({
const lineNum = i + 1;
const isHighlighted = highlights?.includes(lineNum);
const diffType = diffs?.[lineNum];
- const hasDiff = diffType === '+' || diffType === '-';
+ const diff =
+ diffType === '+'
+ ? 'added'
+ : diffType === '-'
+ ? 'removed'
+ : undefined;
return (
{diffs && (
-
- {diffType || ' '}
+
+ {diff === 'added'
+ ? '+'
+ : diff === 'removed'
+ ? '-'
+ : ' '}
)}
{showLineNumbers && (