basalt-ui 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -488,6 +488,28 @@ function spaceDecls(space: SpaceValues): string[] {
488
488
  ]
489
489
  }
490
490
 
491
+ /**
492
+ * The `--vx-space-*` names that survive `only: 'core'` — DERIVED from the `SPACE` anchor keys, never
493
+ * hand-listed, so a new `SPACE_STEP` one-off is excluded the moment it is added and a renamed anchor
494
+ * follows automatically. `SPACE` is the generic ladder (the stack rhythm, the two control heights,
495
+ * the row insets); everything else `spaceDecls` emits is named for a basalt COMPONENT and is dead
496
+ * weight outside this framework — including `--vx-space-row-line-height`, which is the NavLink row's
497
+ * line-height rather than a general anchor and so is (deliberately) in neither set.
498
+ */
499
+ const CORE_SPACE_VARS = new Set(
500
+ Object.keys(SPACE).map((key) => `space-${key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}`),
501
+ )
502
+
503
+ /**
504
+ * Does this `spaceDecls` line declare a core anchor? The name is read back out of the emitted line
505
+ * because {@link decl} is the single formatter for every one of them — the shape is fixed at one
506
+ * call site, so parsing it cannot drift, whereas tagging 104 declarations by hand could.
507
+ */
508
+ function isCoreSpaceDecl(line: string): boolean {
509
+ const name = /^ {2}--vx-([\w-]+):/.exec(line)?.[1]
510
+ return name !== undefined && CORE_SPACE_VARS.has(name)
511
+ }
512
+
491
513
  /** The shipped, level-0 spacing values — `frameworkDerived`'s `spaceDecls(...)` call below. No
492
514
  * `createBasaltTheme({ density })` retuning applies here; that path calls `buildDensityCss` with a
493
515
  * different resolved {@link SpaceValues} instead (see `../theme` and `../provider`). */
@@ -504,7 +526,8 @@ const DEFAULT_SPACE_VALUES: SpaceValues = {
504
526
  * Area-gradient strength is a global knob (the theme lab overrides these two on `:root`
505
527
  * to retune every line-area fill live). 0%/0% disables gradients app-wide.
506
528
  */
507
- function frameworkDerived(data: PaletteData): string {
529
+ function frameworkDerived(data: PaletteData, only: 'core' | 'all'): string {
530
+ const space = spaceDecls(DEFAULT_SPACE_VALUES)
508
531
  return [
509
532
  decl('radius-card', `${RADIUS.card}px`),
510
533
  decl('radius-ctrl', `${RADIUS.ctrl}px`),
@@ -519,7 +542,7 @@ function frameworkDerived(data: PaletteData): string {
519
542
  // only in this static block, never in `buildRadiusCss`'s dynamic override (see
520
543
  // `RADIUS_STEP.pill`'s doc).
521
544
  decl('radius-pill', `${RADIUS_STEP.pill}px`),
522
- ...spaceDecls(DEFAULT_SPACE_VALUES),
545
+ ...(only === 'core' ? space.filter(isCoreSpaceDecl) : space),
523
546
  // Article-density Prose measure (docs/CONTENT-SPEC.md §5) — theme-independent, like the radii.
524
547
  decl('prose-measure', '72ch'),
525
548
  // The fill band (see FILL in palette.ts). Emitted on `:root`, NOT per scheme — a filled surface
@@ -585,14 +608,89 @@ export function groupTokens<const T extends SeriesMap>(
585
608
  return seriesTokens(map, `${name}-`)
586
609
  }
587
610
 
611
+ /**
612
+ * The attribute + values the per-scheme blocks key off. Defaults reproduce Mantine's own toggle
613
+ * (`data-mantine-color-scheme` = `dark` / `light`), so a consumer only names the axis it differs
614
+ * on. Supplying this object at all switches the emitter onto the framework-neutral selector shape
615
+ * — see {@link BuildPaletteOpts.scheme}.
616
+ */
617
+ export type ColorSchemeSelector = {
618
+ attribute?: string
619
+ darkValue?: string
620
+ lightValue?: string
621
+ }
622
+
588
623
  /** Options for {@link buildPaletteCss}. */
589
624
  export type BuildPaletteOpts = {
590
625
  /** Extra named series maps to emit under the given prefixes (e.g. `{ '': SERIES, 'activity-': ACTIVITY }`). */
591
626
  groups?: Record<string, SeriesMap>
592
627
  /** Theme-independent derived declarations (gradients, semantic fills) emitted once on `:root`. */
593
628
  derived?: string[]
629
+ /**
630
+ * Attribute + values the per-scheme blocks key off — for a consumer whose scheme toggle is not
631
+ * Mantine's (a static site on `data-theme`, say). Default: Mantine's own
632
+ * `data-mantine-color-scheme` = `dark` / `light`.
633
+ *
634
+ * Setting ANY of `scheme` / `defaultScheme` / `mediaFallback` moves the emitter off the legacy
635
+ * `html[…]` selector onto `:root[…]`. That is deliberate and load-bearing, NOT cosmetic:
636
+ *
637
+ * `:root[data-theme='dark']` 0-2-0 ← what this path emits
638
+ * `html[data-theme='dark']` 0-1-1
639
+ * `[data-theme='dark']` 0-1-0 ← same as a bare `:root`, so source order decides
640
+ *
641
+ * A light-default site whose own `:root` block happens to come last would beat the third form
642
+ * outright — dark mode silently does nothing. `:root[…]` sits above both a bare `:root` and
643
+ * `html[…]`, and carries no assumption about the element the attribute lands on.
644
+ *
645
+ * The no-options path keeps emitting `html[data-mantine-color-scheme='…']` verbatim, because a
646
+ * basalt consumer may already override `--vx-*` under that exact selector — raising basalt's own
647
+ * specificity would silently win over their override.
648
+ */
649
+ scheme?: ColorSchemeSelector
650
+ /**
651
+ * Which scheme rides along on the bare `:root` block — the one that applies when the attribute
652
+ * is absent. Default: `'dark'` (the framework's own default scheme).
653
+ *
654
+ * `'none'` emits no bare-`:root` primitives at all: each scheme gets only its attribute block,
655
+ * and an unattributed document falls back to the theme-independent scalars alone. Pair it with
656
+ * `mediaFallback` unless the consumer always sets the attribute.
657
+ */
658
+ defaultScheme?: 'dark' | 'light' | 'none'
659
+ /**
660
+ * Also emit an `@media (prefers-color-scheme: …)` block for every scheme that is not the bare
661
+ * `:root` default, so a document with no attribute set follows the OS. Default: `false`.
662
+ *
663
+ * The block is a bare `:root` (0-1-0), so an explicit attribute block (0-2-0) always wins over
664
+ * it regardless of source order — the OS preference is a fallback, never an override.
665
+ */
666
+ mediaFallback?: boolean
667
+ /**
668
+ * How much of the SPACING set to emit. Default: `'all'` (every one of the 104 `--vx-space-*`
669
+ * declarations the framework's own components read).
670
+ *
671
+ * `'core'` keeps the 9 generic anchors — the stack rhythm, the two control heights, the row
672
+ * insets — and drops the 95 named for a specific basalt component
673
+ * (`--vx-space-agent-transcript-inset`, `--vx-space-toc-sub-indent`, …), taking the emitted set
674
+ * from 197 variables to 102. Nothing outside basalt's own React components reads those, so a
675
+ * framework-free consumer is carrying them for nothing.
676
+ *
677
+ * This is a SPACING filter only: color, radius, shadow, type and status variables are identical
678
+ * in both modes. The partition is derived from the `SPACE` key set rather than a maintained list
679
+ * — see {@link CORE_SPACE_VARS}.
680
+ */
681
+ only?: 'core' | 'all'
594
682
  }
595
683
 
684
+ /** Mantine's own color-scheme attribute — the default {@link ColorSchemeSelector.attribute}. */
685
+ const MANTINE_SCHEME_ATTRIBUTE = 'data-mantine-color-scheme'
686
+
687
+ /** Indent an already-2-space-indented declaration block one level deeper (for `@media` nesting). */
688
+ const indent = (block: string): string =>
689
+ block
690
+ .split('\n')
691
+ .map((line) => (line === '' ? line : ` ${line}`))
692
+ .join('\n')
693
+
596
694
  /**
597
695
  * Emit the `--vx-*` stylesheet: a `:root` block of theme-independent scalars, then the
598
696
  * dark (default) and light primitive blocks under `html[data-mantine-color-scheme='…']`.
@@ -608,6 +706,14 @@ export type BuildPaletteOpts = {
608
706
  *
609
707
  * Dark is the default (`:root`) since the framework defaults to dark; the
610
708
  * `[data-mantine-color-scheme]` selectors track Mantine's toggle on `<html>`.
709
+ *
710
+ * A non-Mantine consumer retargets all three of those choices — the attribute, which scheme rides
711
+ * the bare `:root`, and whether the OS preference is honoured — via `opts.scheme` /
712
+ * `opts.defaultScheme` / `opts.mediaFallback`. Setting any of them also moves the per-scheme
713
+ * selector from `html[…]` to the framework-neutral, higher-specificity `:root[…]`; see
714
+ * `BuildPaletteOpts.scheme` for why that is not cosmetic. With none of them set the output is
715
+ * byte-identical to what the framework has always emitted (pinned by
716
+ * `tests/palette-css.test.ts`).
611
717
  */
612
718
  export function buildPaletteCss(
613
719
  opts: BuildPaletteOpts = {},
@@ -615,7 +721,7 @@ export function buildPaletteCss(
615
721
  ): string {
616
722
  const groups = opts.groups ?? {}
617
723
  const extraDerived = (opts.derived ?? []).map((d) => ` ${d}`).join('\n')
618
- const derivedBlock = frameworkDerived(data)
724
+ const derivedBlock = frameworkDerived(data, opts.only ?? 'all')
619
725
  const derived = extraDerived ? `${derivedBlock}\n${extraDerived}` : derivedBlock
620
726
  const side = (s: Side): string => {
621
727
  const extra = Object.entries(groups)
@@ -624,9 +730,11 @@ export function buildPaletteCss(
624
730
  const framework = frameworkPrimitives(s, data)
625
731
  return extra ? `${framework}\n${extra}` : framework
626
732
  }
627
- return `:root {
628
- ${derived}
629
- }
733
+ const rootBlock = `:root {\n${derived}\n}`
734
+
735
+ // The legacy shape — kept verbatim, and only reachable when the caller asked for nothing.
736
+ if (opts.scheme === undefined && opts.defaultScheme === undefined && !opts.mediaFallback) {
737
+ return `${rootBlock}
630
738
  :root,
631
739
  html[data-mantine-color-scheme='dark'] {
632
740
  ${side('dark')}
@@ -634,6 +742,34 @@ ${side('dark')}
634
742
  html[data-mantine-color-scheme='light'] {
635
743
  ${side('light')}
636
744
  }`
745
+ }
746
+
747
+ const attribute = opts.scheme?.attribute ?? MANTINE_SCHEME_ATTRIBUTE
748
+ const value: Record<Side, string> = {
749
+ dark: opts.scheme?.darkValue ?? 'dark',
750
+ light: opts.scheme?.lightValue ?? 'light',
751
+ }
752
+ const defaultScheme = opts.defaultScheme ?? 'dark'
753
+ // Default scheme first, so its attribute block can absorb the bare `:root` instead of repeating
754
+ // ~40 declarations; `'none'` keeps the framework's dark-then-light order.
755
+ const order: Side[] = defaultScheme === 'light' ? ['light', 'dark'] : ['dark', 'light']
756
+
757
+ const blocks = [rootBlock]
758
+ for (const s of order) {
759
+ const selector =
760
+ s === defaultScheme
761
+ ? `:root,\n:root[${attribute}='${value[s]}']`
762
+ : `:root[${attribute}='${value[s]}']`
763
+ // The OS fallback for a scheme goes immediately before that scheme's own attribute block. It is
764
+ // a bare `:root` (0-1-0), so the attribute block (0-2-0) outranks it on specificity rather than
765
+ // on order — an explicit toggle always beats the OS preference, and the OS preference always
766
+ // beats the hardcoded default that a bare `:root` above it carries.
767
+ if (opts.mediaFallback === true && s !== defaultScheme) {
768
+ blocks.push(`@media (prefers-color-scheme: ${s}) {\n :root {\n${indent(side(s))}\n }\n}`)
769
+ }
770
+ blocks.push(`${selector} {\n${side(s)}\n}`)
771
+ }
772
+ return blocks.join('\n')
637
773
  }
638
774
 
639
775
  /**