@diegopluna/narvi 0.3.0 → 0.3.1

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.
package/README.md CHANGED
@@ -135,7 +135,9 @@ publish succeeds, so a failed run can be run again.
135
135
  - **The Vite plugin** in `src/lib/vite.ts` is a preset over `@stylexjs/unplugin`. It puts every
136
136
  style in one cascade layer, `narvi`, so unlayered CSS beats it, and declares `narvi.base` first
137
137
  inside it, so `base.css` stays below the components whichever stylesheet loads first. `narvi({ tailwind: true })`
138
- orders that layer between Tailwind's `base` and its `components` and `utilities`. It also says so
138
+ orders that layer between Tailwind's `base` and its `components` and `utilities`, and starts
139
+ every stylesheet that names one of those layers with the order, so it holds whichever loads
140
+ first. It also says so
139
141
  when it is listed before the Svelte plugin, and targets browsers that have `light-dark()`, so
140
142
  Vite does not rewrite it in the app's own stylesheet.
141
143
 
@@ -18,7 +18,7 @@
18
18
  import { getSettingsGroupContext } from '../settings-group/context.js';
19
19
 
20
20
  export type SettingsRowControl = {
21
- /** For the control's `id`, which the label's `for` names. */
21
+ /** For the control's `id`, which the label's `for` names: `controlId` when it is set. */
22
22
  id: string;
23
23
  /** The label's id: a group's `aria-labelledby` when `labelFor` is off. */
24
24
  labelId: string;
@@ -46,6 +46,12 @@
46
46
  * its own label does; the label is then plain text, and its id is `labelId`.
47
47
  */
48
48
  labelFor?: boolean | undefined;
49
+ /**
50
+ * The control's id, when the app names it: one that search, a link or a test finds it by.
51
+ * The label's `for` names it, and the row hands it to the control as `id`. Without it the
52
+ * row makes one.
53
+ */
54
+ controlId?: string | undefined;
49
55
  /** Lights the row, as when search lands on it. */
50
56
  highlighted?: boolean | undefined;
51
57
  /** Shown under the row, announced at once. */
@@ -125,6 +131,7 @@
125
131
  description,
126
132
  stacked = false,
127
133
  labelFor = true,
134
+ controlId,
128
135
  highlighted = false,
129
136
  error,
130
137
  warning,
@@ -142,7 +149,11 @@
142
149
  .filter(Boolean)
143
150
  .join(' ') || undefined
144
151
  );
145
- const control = $derived({ id: `${uid}-control`, labelId: `${uid}-label`, describedBy });
152
+ const control = $derived({
153
+ id: controlId ?? `${uid}-control`,
154
+ labelId: `${uid}-label`,
155
+ describedBy
156
+ });
146
157
  </script>
147
158
 
148
159
  <div
@@ -2,7 +2,7 @@ import type { Snippet } from 'svelte';
2
2
  import type { HTMLAttributes } from 'svelte/elements';
3
3
  import { type Sx } from '../../internal/sx.js';
4
4
  export type SettingsRowControl = {
5
- /** For the control's `id`, which the label's `for` names. */
5
+ /** For the control's `id`, which the label's `for` names: `controlId` when it is set. */
6
6
  id: string;
7
7
  /** The label's id: a group's `aria-labelledby` when `labelFor` is off. */
8
8
  labelId: string;
@@ -29,6 +29,12 @@ export type SettingsRowProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' |
29
29
  * its own label does; the label is then plain text, and its id is `labelId`.
30
30
  */
31
31
  labelFor?: boolean | undefined;
32
+ /**
33
+ * The control's id, when the app names it: one that search, a link or a test finds it by.
34
+ * The label's `for` names it, and the row hands it to the control as `id`. Without it the
35
+ * row makes one.
36
+ */
37
+ controlId?: string | undefined;
32
38
  /** Lights the row, as when search lands on it. */
33
39
  highlighted?: boolean | undefined;
34
40
  /** Shown under the row, announced at once. */
@@ -1,7 +1,9 @@
1
1
  <!--
2
- A row that holds other rows: a project and its threads. It folds, and its rows
3
- are set in under it. It is a row in the list like any other, so the arrow keys
4
- pass through it; the right arrow opens it and the left one shuts it.
2
+ A row that holds other rows: a project and its threads. Its chevron comes first, on the
3
+ edge where the other rows' icons sit; then its mark, if it has one, and its name. It folds,
4
+ and its rows are set in under it, their titles starting where its name does. It is a row in
5
+ the list like any other, so the arrow keys pass through it; the right arrow opens it and the
6
+ left one shuts it.
5
7
  -->
6
8
  <script lang="ts" module>
7
9
  import * as stylex from '@stylexjs/stylex';
@@ -38,7 +40,7 @@
38
40
  width: '100%',
39
41
  margin: 0,
40
42
  paddingBlock: '0.375rem',
41
- // The same as Sidebar.Item, so a folder's mark and a row's icon share one edge. A
43
+ // The same as Sidebar.Item, so a folder's chevron and a row's icon share one edge. A
42
44
  // folder in a folder is set in as a row is.
43
45
  paddingInline: '0.5rem',
44
46
  paddingInlineStart: 'calc(0.5rem + var(--narvi-sidebar-depth, 0) * 1.5rem)',
@@ -65,12 +67,13 @@
65
67
  transitionDuration: motion.fast,
66
68
  transitionTimingFunction: 'ease'
67
69
  },
68
- // After the name, as in Sidebar.Group, and pulled a little closer to it
70
+ // In a row icon's slot and at its size, in the muted text: a control, not a decoration
69
71
  chevron: {
70
72
  display: 'grid',
73
+ placeItems: 'center',
71
74
  flexShrink: 0,
72
- marginInlineStart: '-0.25rem',
73
- color: colors.textSubtle
75
+ width: '1rem',
76
+ color: colors.textMuted
74
77
  },
75
78
  lead: { display: 'grid', placeItems: 'center', flexShrink: 0, width: '1rem' },
76
79
  grow: { flexGrow: 1 },
@@ -114,8 +117,10 @@
114
117
  }: SidebarFolderProps = $props();
115
118
 
116
119
  const uid = $props.id();
117
- const depth = getSidebarDepth();
118
- setSidebarDepth(depth + 1);
120
+ const parent = getSidebarDepth();
121
+ const depth = $derived(parent());
122
+ // Its rows start where its name does: past the chevron, and past the mark when it has one
123
+ setSidebarDepth(() => parent() + (leading ? 2 : 1));
119
124
  </script>
120
125
 
121
126
  <li data-slot="sidebar-folder" {...rest} {...attrs([styles.folder, sx], className)}>
@@ -135,9 +140,9 @@
135
140
  event.preventDefault();
136
141
  }}
137
142
  >
143
+ <span {...stylex.attrs(styles.chevron)}><Chevron {open} size={16} /></span>
138
144
  {#if leading}<span {...stylex.attrs(styles.lead)}>{@render leading()}</span>{/if}
139
145
  <span {...stylex.attrs(styles.label)}>{label}</span>
140
- <span {...stylex.attrs(styles.chevron)}><Chevron {open} size={12} /></span>
141
146
  <span {...stylex.attrs(styles.grow)}></span>
142
147
  {#if trailing}<span {...stylex.attrs(styles.trail)}>{@render trailing()}</span>{/if}
143
148
  </button>
@@ -91,7 +91,8 @@
91
91
  },
92
92
  noLeadCompact: { gridTemplateAreas: '"title trail"' },
93
93
  muted: { color: colors.textMuted },
94
- // The open row: a flat fill and a heavier title
94
+ // The open row: a flat fill. The title keeps its weight, so it cuts off where it did
95
+ // and does not jump as the person moves between rows.
95
96
  current: {
96
97
  color: colors.text,
97
98
  backgroundColor: { default: colors.fillSelected, ':hover': colors.fillSelected }
@@ -113,7 +114,6 @@
113
114
  textOverflow: 'ellipsis',
114
115
  whiteSpace: 'nowrap'
115
116
  },
116
- titleCurrent: { fontWeight: font.weightSemibold },
117
117
  trail: {
118
118
  gridArea: 'trail',
119
119
  display: 'inline-flex',
@@ -200,7 +200,8 @@
200
200
  ...rest
201
201
  }: SidebarItemProps = $props();
202
202
 
203
- const depth = getSidebarDepth();
203
+ const getDepth = getSidebarDepth();
204
+ const depth = $derived(getDepth());
204
205
 
205
206
  const rowAttrs = $derived(
206
207
  attrs(
@@ -220,8 +221,7 @@
220
221
 
221
222
  {#snippet inside()}
222
223
  {#if leading}<span {...stylex.attrs(styles.lead)}>{@render leading()}</span>{/if}
223
- <span {...stylex.attrs(styles.title, current && styles.titleCurrent)}>{@render children?.()}</span
224
- >
224
+ <span {...stylex.attrs(styles.title)}>{@render children?.()}</span>
225
225
  {#if trailing}
226
226
  <span {...stylex.attrs(styles.trail, actions && styles.trailHides)}>{@render trailing()}</span>
227
227
  {/if}
@@ -1,8 +1,10 @@
1
1
  /**
2
- * How many folders a row sits in. A row is set in by its text, not its box: its highlight
3
- * runs the sidebar's width, as its folder's does, and its title starts where the folder's
4
- * name does: 1.5rem a level, a folder's mark (1rem) and the gap after it (0.5rem). The rows
5
- * write that in their own styles, as StyleX reads only its own files' constants.
2
+ * How far a row is set in, in steps of 1.5rem: an icon's slot (1rem) and the gap after it
3
+ * (0.5rem). A row is set in by its text, not its box: its highlight runs the sidebar's width,
4
+ * as its folder's does, and its title starts where the folder's name does. A folder takes a
5
+ * step for its chevron, and another for its mark when it has one. The rows write the step in
6
+ * their own styles, as StyleX reads only its own files' constants.
6
7
  */
7
- export declare function getSidebarDepth(): number;
8
- export declare function setSidebarDepth(depth: number): void;
8
+ export declare function getSidebarDepth(): () => number;
9
+ /** For a folder's rows: a getter, as whether the folder has a mark can change. */
10
+ export declare function setSidebarDepth(depth: () => number): void;
@@ -1,14 +1,16 @@
1
1
  import { getContext, setContext } from 'svelte';
2
2
  const KEY = Symbol('narvi.sidebar.depth');
3
3
  /**
4
- * How many folders a row sits in. A row is set in by its text, not its box: its highlight
5
- * runs the sidebar's width, as its folder's does, and its title starts where the folder's
6
- * name does: 1.5rem a level, a folder's mark (1rem) and the gap after it (0.5rem). The rows
7
- * write that in their own styles, as StyleX reads only its own files' constants.
4
+ * How far a row is set in, in steps of 1.5rem: an icon's slot (1rem) and the gap after it
5
+ * (0.5rem). A row is set in by its text, not its box: its highlight runs the sidebar's width,
6
+ * as its folder's does, and its title starts where the folder's name does. A folder takes a
7
+ * step for its chevron, and another for its mark when it has one. The rows write the step in
8
+ * their own styles, as StyleX reads only its own files' constants.
8
9
  */
9
10
  export function getSidebarDepth() {
10
- return getContext(KEY) ?? 0;
11
+ return getContext(KEY) ?? (() => 0);
11
12
  }
13
+ /** For a folder's rows: a getter, as whether the folder has a mark can change. */
12
14
  export function setSidebarDepth(depth) {
13
15
  setContext(KEY, depth);
14
16
  }
package/dist/vite.d.ts CHANGED
@@ -12,12 +12,8 @@ export type NarviOptions = StylexOptions & {
12
12
  * For apps that use Tailwind CSS 4. Places Narvi's layer after Tailwind's
13
13
  * `base` (so Preflight does not undo Narvi) and before its `components` and
14
14
  * `utilities` (so a class such as `hidden sm:inline-flex` overrides a
15
- * component). Also add this line to the top of your stylesheet, before
16
- * `@import "tailwindcss"`:
17
- *
18
- * ```css
19
- * @layer theme, base, narvi, components, utilities;
20
- * ```
15
+ * component). Every stylesheet that names one of these layers starts with the
16
+ * order (`TAILWIND_ORDER`), so it holds whichever the browser reads first.
21
17
  */
22
18
  tailwind?: boolean | undefined;
23
19
  };
@@ -30,6 +26,19 @@ export declare const CSS_TARGET: string[];
30
26
  export declare const NARVI_LAYER = "narvi";
31
27
  /** The layer inside it that holds `@diegopluna/narvi/base.css`, below every component. */
32
28
  export declare const BASE_LAYER = "narvi.base";
29
+ /**
30
+ * The order of Narvi's layer among Tailwind's, as the first rule of a stylesheet. With
31
+ * `narvi({ tailwind: true })` the plugin writes it at the top of each one that names a layer.
32
+ */
33
+ export declare const TAILWIND_ORDER = "@layer theme, base, narvi.base, components, utilities;";
34
+ /**
35
+ * Puts Tailwind's layer order, with Narvi's in it, at the top of a stylesheet that names one of
36
+ * those layers. The browser orders layers as it first meets their names, and the first stylesheet
37
+ * on the page need not be the app's: a component's own stylesheet, such as the one that sizes
38
+ * icons, can load first. Naming `narvi` there before Tailwind's layers put Narvi below `base`,
39
+ * where Preflight's reset undid it. Returns `undefined` when the stylesheet is left as it is.
40
+ */
41
+ export declare function orderLayers(code: string, id: string): string | undefined;
33
42
  /**
34
43
  * Vite plugin that compiles Narvi's styles (and any StyleX you write yourself).
35
44
  *
package/dist/vite.js CHANGED
@@ -11,6 +11,31 @@ export const CSS_TARGET = ['chrome123', 'edge123', 'firefox120', 'safari17.5'];
11
11
  export const NARVI_LAYER = 'narvi';
12
12
  /** The layer inside it that holds `@diegopluna/narvi/base.css`, below every component. */
13
13
  export const BASE_LAYER = `${NARVI_LAYER}.base`;
14
+ /**
15
+ * The order of Narvi's layer among Tailwind's, as the first rule of a stylesheet. With
16
+ * `narvi({ tailwind: true })` the plugin writes it at the top of each one that names a layer.
17
+ */
18
+ export const TAILWIND_ORDER = `@layer theme, base, ${BASE_LAYER}, components, utilities;`;
19
+ // A stylesheet Vite compiles, including a Svelte component's (`?svelte&type=style&lang.css`),
20
+ // and not one imported as a URL or as text.
21
+ const CSS_RE = /\.(?:css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
22
+ const NOT_CSS_RE = /[?&](?:raw|url|worker|sharedworker)\b/;
23
+ // A rule or a statement that names Narvi's layer or one of Tailwind's.
24
+ const NAMES_LAYER_RE = /@layer\s+(?:narvi|theme|base|components|utilities)\b/;
25
+ /**
26
+ * Puts Tailwind's layer order, with Narvi's in it, at the top of a stylesheet that names one of
27
+ * those layers. The browser orders layers as it first meets their names, and the first stylesheet
28
+ * on the page need not be the app's: a component's own stylesheet, such as the one that sizes
29
+ * icons, can load first. Naming `narvi` there before Tailwind's layers put Narvi below `base`,
30
+ * where Preflight's reset undid it. Returns `undefined` when the stylesheet is left as it is.
31
+ */
32
+ export function orderLayers(code, id) {
33
+ if (!CSS_RE.test(id) || NOT_CSS_RE.test(id) || !NAMES_LAYER_RE.test(code))
34
+ return undefined;
35
+ // `@charset` must stay first; the order may come before any `@import`
36
+ const charset = /^@charset\s+"[^"]*";\s*/.exec(code)?.[0] ?? '';
37
+ return `${charset}${TAILWIND_ORDER}\n${code.slice(charset.length)}`;
38
+ }
14
39
  /**
15
40
  * Vite plugin that compiles Narvi's styles (and any StyleX you write yourself).
16
41
  *
@@ -55,6 +80,8 @@ export function narvi({ tailwind = false, ...options } = {}) {
55
80
  const configureServer = plugin.configureServer;
56
81
  const config = plugin.config;
57
82
  const configResolved = plugin.configResolved;
83
+ // StyleX's hook is a plain function, not an object with a `handler`
84
+ const transform = plugin.transform;
58
85
  const self = {
59
86
  ...plugin,
60
87
  // Vite lowers the CSS it bundles for its default targets, which are older than
@@ -72,6 +99,17 @@ export function narvi({ tailwind = false, ...options } = {}) {
72
99
  assertAfterSvelte(config.plugins, self);
73
100
  return configResolved?.call(this, config);
74
101
  },
102
+ // StyleX compiles scripts. With Tailwind, a stylesheet that names a layer (one of Narvi's
103
+ // components', the app's, Tailwind's own output) is given the order first. Vite hands it
104
+ // here after compiling it, and before turning it into a module or a bundle.
105
+ transform(code, id, options) {
106
+ if (tailwind) {
107
+ const ordered = orderLayers(code, id);
108
+ if (ordered !== undefined)
109
+ return { code: ordered, map: null };
110
+ }
111
+ return transform?.call(this, code, id, options);
112
+ },
75
113
  // The StyleX plugin defaults to `enforce: 'pre'`, which would hand it raw
76
114
  // `.svelte` source. Dropping `enforce` lets the Svelte compiler go first.
77
115
  enforce: undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopluna/narvi",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "packageManager": "pnpm@12.18.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,7 +12,7 @@ metadata:
12
12
  Grid, and Narvi's conventions for colour, labels and copy.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.3.0'
15
+ library_version: '0.3.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -219,6 +219,9 @@ of the heading), which lays them on one card with a line between each, and each
219
219
  - The row's label is a `<label for>` the control. For a group (`SegmentedControl`, radios,
220
220
  several buttons) or a control that labels itself (a `Switch` with children), set
221
221
  `labelFor={false}` and pass `labelId` to the control's `aria-labelledby`.
222
+ - The row makes the control's id. Where the app needs a fixed one (search, a deep link, a
223
+ test), pass `controlId="settings-branch-prefix"`: the label's `for` names it and `children`
224
+ receives it as `id`. Don't keep a row of your own for that.
222
225
  - Attributes such as `id`, `tabindex` and `data-*` go on the row's element (and on the group's
223
226
  `<section>`), so search can scroll to one and focus it. Set `highlighted` for a moment as it
224
227
  lands to light it.
@@ -319,8 +322,12 @@ Parts of the shell that are easy to redraw by hand, and shouldn't be:
319
322
  - **A workspace switcher:** `Sidebar.Switcher` is a row with the workspace's mark (`leading`),
320
323
  its name and a selector glyph. Put it in a `Menu.Trigger`'s `child` and spread the props:
321
324
  `{#snippet child({ props })}<Sidebar.Switcher {...props}>…</Sidebar.Switcher>{/snippet}`.
322
- - **Rows in folders** are set in by their text, not their box: give a folder's mark as its
323
- `leading` and its rows line up on their own. Don't pad rows or folders yourself.
325
+ - **Rows in folders** are set in by their text, not their box. A folder's chevron comes first,
326
+ on the edge of the rows' icons; give its mark as `leading` and it follows the chevron. Its
327
+ rows line up on their own, their titles under the folder's name, with a mark or without.
328
+ Don't pad rows or folders yourself.
329
+ - **The open row** (`current`) is a flat fill; its title keeps its weight. Don't bold it
330
+ yourself: a heavier title is wider, so it cuts off at a different point as the person moves.
324
331
  - **A listbox of results** from Sidebar rows: `<Sidebar.Item as="div" role="option" aria-selected>`
325
332
  inside your `role="listbox"`, so no list item stands between the box and its options.
326
333
  - **TabStrip** tabs narrow to an ellipsis before the strip scrolls. With a `leading` icon, a
@@ -12,7 +12,7 @@ metadata:
12
12
  patching the DOM, and decide which links and images text from a model may load.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.3.0'
15
+ library_version: '0.3.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -11,7 +11,7 @@ metadata:
11
11
  4 when the app uses it.
12
12
  type: lifecycle
13
13
  library: '@diegopluna/narvi'
14
- library_version: '0.3.0'
14
+ library_version: '0.3.1'
15
15
  sources:
16
16
  - src/lib/vite.ts
17
17
  - src/lib/base.css
@@ -132,7 +132,7 @@ need to spread a prop only when it is set. That holds for the parts Narvi takes
132
132
  ## With Tailwind CSS 4
133
133
 
134
134
  Out of the box, Narvi's layer comes after Tailwind's, so a utility such as `hidden` on a Button
135
- changes nothing. Both of these changes are needed:
135
+ changes nothing. Set the plugin's `tailwind` option:
136
136
 
137
137
  ```ts
138
138
  // vite.config.ts
@@ -146,15 +146,13 @@ export default defineConfig({
146
146
  });
147
147
  ```
148
148
 
149
- ```css
150
- /* src/app.css: the order line comes first, before any @import */
151
- @layer theme, base, narvi, components, utilities;
152
- @import 'tailwindcss';
153
- ```
154
-
155
- The browser fixes the layer order from whichever stylesheet it reads first, and in development
156
- that can be either one, so the plugin option and the CSS line are both needed. Narvi still beats
157
- Preflight. Utilities replace whole properties: `p-0` on a Card removes its padding, not a part of it.
149
+ The browser fixes the layer order from whichever stylesheet names the layers first, and that can
150
+ be one of Narvi's components' (the one that sizes icons, from 0.3) rather than the app's. With the
151
+ option, the plugin starts every stylesheet that names Narvi's layer or one of Tailwind's with
152
+ `@layer theme, base, narvi.base, components, utilities;` (exported as `TAILWIND_ORDER`), so the
153
+ order holds whichever loads first: Narvi beats Preflight, and utilities beat Narvi. No line in
154
+ `app.css` is needed; one written for an earlier version does no harm. Utilities replace whole
155
+ properties: `p-0` on a Card removes its padding, not a part of it.
158
156
 
159
157
  ## Common mistakes
160
158
 
@@ -12,7 +12,7 @@ metadata:
12
12
  hover, motion and forced-colour preferences.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.3.0'
15
+ library_version: '0.3.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -11,7 +11,7 @@ metadata:
11
11
  menus and dialogs, and resolve token colours for drawing code that cannot read CSS variables.
12
12
  type: core
13
13
  library: '@diegopluna/narvi'
14
- library_version: '0.3.0'
14
+ library_version: '0.3.1'
15
15
  requires:
16
16
  - setup
17
17
  sources: