@diegopluna/narvi 0.1.0 → 0.2.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.
- package/README.md +102 -41
- package/dist/base.css +2 -2
- package/dist/color.d.ts +1 -1
- package/dist/color.js +1 -1
- package/dist/components/activity/ActivityContent.svelte +3 -15
- package/dist/components/activity/ActivityStep.svelte +12 -12
- package/dist/components/activity/ActivityThought.svelte +4 -4
- package/dist/components/activity/ActivityTrigger.svelte +3 -3
- package/dist/components/app-shell/AppShellBar.svelte +89 -0
- package/dist/components/app-shell/AppShellBar.svelte.d.ts +20 -0
- package/dist/components/app-shell/AppShellHeader.svelte +17 -16
- package/dist/components/app-shell/AppShellHeader.svelte.d.ts +2 -2
- package/dist/components/app-shell/AppShellMain.svelte +4 -20
- package/dist/components/app-shell/AppShellPanel.svelte +21 -19
- package/dist/components/app-shell/AppShellPanes.svelte +58 -0
- package/dist/components/app-shell/AppShellPanes.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRail.svelte +85 -0
- package/dist/components/app-shell/AppShellRail.svelte.d.ts +11 -0
- package/dist/components/app-shell/AppShellRoot.svelte +50 -24
- package/dist/components/app-shell/AppShellRoot.svelte.d.ts +8 -6
- package/dist/components/app-shell/AppShellSheet.svelte +95 -0
- package/dist/components/app-shell/AppShellSheet.svelte.d.ts +17 -0
- package/dist/components/app-shell/AppShellSidebar.svelte +37 -61
- package/dist/components/app-shell/AppShellSidebar.svelte.d.ts +1 -5
- package/dist/components/app-shell/AppShellToggle.svelte +62 -23
- package/dist/components/app-shell/AppShellToggle.svelte.d.ts +2 -0
- package/dist/components/app-shell/context.d.ts +6 -0
- package/dist/components/app-shell/index.d.ts +9 -1
- package/dist/components/app-shell/index.js +5 -1
- package/dist/components/approval/Approval.svelte +91 -19
- package/dist/components/approval/Approval.svelte.d.ts +7 -0
- package/dist/components/avatar/AvatarFallback.svelte +1 -2
- package/dist/components/avatar/AvatarRoot.svelte +2 -2
- package/dist/components/badge/Badge.svelte +5 -5
- package/dist/components/button/Button.svelte +219 -40
- package/dist/components/button/Button.svelte.d.ts +38 -2
- package/dist/components/card/Card.svelte +15 -26
- package/dist/components/change-stats/ChangeStats.svelte +1 -2
- package/dist/components/changes-summary/ChangesSummary.svelte +14 -15
- package/dist/components/checkbox/Checkbox.svelte +18 -15
- package/dist/components/checkbox/Checkbox.svelte.d.ts +5 -0
- package/dist/components/code/Code.svelte +1 -2
- package/dist/components/collapsible/CollapsibleTrigger.svelte +2 -2
- package/dist/components/command/CommandItem.svelte +1 -1
- package/dist/components/command/styles.d.ts +3 -2
- package/dist/components/command/styles.js +13 -10
- package/dist/components/composer/ComposerAttachButton.svelte +1 -1
- package/dist/components/composer/ComposerAttachment.svelte +9 -12
- package/dist/components/composer/ComposerModelPicker.svelte +18 -20
- package/dist/components/composer/ComposerRoot.svelte +8 -7
- package/dist/components/composer/ComposerSubmit.svelte +8 -15
- package/dist/components/composer/ComposerTextarea.svelte +1 -1
- package/dist/components/composer/ComposerToggle.svelte +9 -11
- package/dist/components/composer/ComposerTray.svelte +78 -0
- package/dist/components/composer/ComposerTray.svelte.d.ts +15 -0
- package/dist/components/composer/index.d.ts +3 -1
- package/dist/components/composer/index.js +3 -2
- package/dist/components/conversation/ConversationContent.svelte +3 -2
- package/dist/components/conversation/ConversationEmpty.svelte +1 -1
- package/dist/components/conversation/ConversationScrollButton.svelte +3 -3
- package/dist/components/dialog/ConfirmDialog.svelte +1 -1
- package/dist/components/dialog/styles.d.ts +2 -3
- package/dist/components/dialog/styles.js +5 -6
- package/dist/components/divider/Divider.svelte +3 -3
- package/dist/components/empty-state/EmptyState.svelte +4 -5
- package/dist/components/field/Field.svelte +4 -4
- package/dist/components/frame/Frame.svelte +6 -7
- package/dist/components/frame/Frame.svelte.d.ts +1 -1
- package/dist/components/heading/Heading.svelte +2 -2
- package/dist/components/kbd/Kbd.svelte +12 -7
- package/dist/components/link/Link.svelte +7 -3
- package/dist/components/link/Link.svelte.d.ts +4 -1
- package/dist/components/markdown/Markdown.svelte +714 -0
- package/dist/components/markdown/Markdown.svelte.d.ts +80 -0
- package/dist/components/markdown/MarkdownBlock.svelte +231 -0
- package/dist/components/markdown/MarkdownBlock.svelte.d.ts +11 -0
- package/dist/components/markdown/MarkdownBlocks.svelte +115 -0
- package/dist/components/markdown/MarkdownBlocks.svelte.d.ts +15 -0
- package/dist/components/markdown/MarkdownCode.svelte +583 -0
- package/dist/components/markdown/MarkdownCode.svelte.d.ts +23 -0
- package/dist/components/markdown/MarkdownDiagram.svelte +310 -0
- package/dist/components/markdown/MarkdownDiagram.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownFile.svelte +134 -0
- package/dist/components/markdown/MarkdownFile.svelte.d.ts +19 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte +61 -0
- package/dist/components/markdown/MarkdownFootnotes.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownFresh.svelte +69 -0
- package/dist/components/markdown/MarkdownFresh.svelte.d.ts +6 -0
- package/dist/components/markdown/MarkdownHtml.svelte +72 -0
- package/dist/components/markdown/MarkdownHtml.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownImage.svelte +150 -0
- package/dist/components/markdown/MarkdownImage.svelte.d.ts +8 -0
- package/dist/components/markdown/MarkdownInlines.svelte +225 -0
- package/dist/components/markdown/MarkdownInlines.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownLink.svelte +120 -0
- package/dist/components/markdown/MarkdownLink.svelte.d.ts +25 -0
- package/dist/components/markdown/MarkdownMath.svelte +138 -0
- package/dist/components/markdown/MarkdownMath.svelte.d.ts +7 -0
- package/dist/components/markdown/MarkdownTable.svelte +265 -0
- package/dist/components/markdown/MarkdownTable.svelte.d.ts +17 -0
- package/dist/components/markdown/caret.d.ts +14 -0
- package/dist/components/markdown/caret.js +67 -0
- package/dist/components/markdown/context.d.ts +93 -0
- package/dist/components/markdown/context.js +128 -0
- package/dist/components/markdown/copy.d.ts +13 -0
- package/dist/components/markdown/copy.js +74 -0
- package/dist/components/markdown/hidden.d.ts +12 -0
- package/dist/components/markdown/hidden.js +31 -0
- package/dist/components/markdown/kept.d.ts +32 -0
- package/dist/components/markdown/kept.js +71 -0
- package/dist/components/markdown/labels.d.ts +46 -0
- package/dist/components/markdown/labels.js +35 -0
- package/dist/components/markdown/save.d.ts +11 -0
- package/dist/components/markdown/save.js +58 -0
- package/dist/components/markdown/scrollable.d.ts +24 -0
- package/dist/components/markdown/scrollable.js +152 -0
- package/dist/components/markdown/static.d.ts +37 -0
- package/dist/components/markdown/static.js +233 -0
- package/dist/components/markdown/styles.d.ts +328 -0
- package/dist/components/markdown/styles.js +302 -0
- package/dist/components/menu/MenuRadioItem.svelte +3 -3
- package/dist/components/menu/index.d.ts +23 -0
- package/dist/components/menu/index.js +3 -1
- package/dist/components/message/MessageAction.svelte +22 -2
- package/dist/components/message/MessageAction.svelte.d.ts +9 -0
- package/dist/components/message/MessageContent.svelte +2 -3
- package/dist/components/message/MessageCopy.svelte +62 -3
- package/dist/components/message/MessageCopy.svelte.d.ts +17 -1
- package/dist/components/message/MessageHeader.svelte +3 -3
- package/dist/components/message/MessagePending.svelte +1 -1
- package/dist/components/message/MessageTime.svelte +1 -1
- package/dist/components/notice/Notice.svelte +14 -10
- package/dist/components/plan/Plan.svelte +22 -17
- package/dist/components/progress/Progress.svelte +31 -33
- package/dist/components/questionnaire/Questionnaire.svelte +19 -22
- package/dist/components/queued-message/QueuedMessage.svelte +14 -7
- package/dist/components/rail/RailItem.svelte +37 -34
- package/dist/components/rail/RailItem.svelte.d.ts +3 -1
- package/dist/components/rail/RailRoot.svelte +18 -4
- package/dist/components/rail/RailRoot.svelte.d.ts +1 -1
- package/dist/components/rail/RailSeparator.svelte +17 -0
- package/dist/components/rail/RailSeparator.svelte.d.ts +18 -0
- package/dist/components/rail/index.d.ts +2 -1
- package/dist/components/rail/index.js +2 -1
- package/dist/components/reasoning/ReasoningContent.svelte +4 -8
- package/dist/components/reasoning/ReasoningTrigger.svelte +2 -2
- package/dist/components/resize-handle/ResizeHandle.svelte +2 -3
- package/dist/components/segmented/SegmentedControl.svelte +7 -8
- package/dist/components/select/Select.svelte +1 -1
- package/dist/components/separator/Separator.svelte +1 -1
- package/dist/components/settings-row/SettingsRow.svelte +5 -5
- package/dist/components/shimmer/Shimmer.svelte +34 -21
- package/dist/components/sidebar/SidebarContent.svelte +1 -1
- package/dist/components/sidebar/SidebarFolder.svelte +142 -0
- package/dist/components/sidebar/SidebarFolder.svelte.d.ts +19 -0
- package/dist/components/sidebar/SidebarFooter.svelte +1 -1
- package/dist/components/sidebar/SidebarGroup.svelte +36 -32
- package/dist/components/sidebar/SidebarGroup.svelte.d.ts +3 -3
- package/dist/components/sidebar/SidebarHeader.svelte +12 -6
- package/dist/components/sidebar/SidebarHeader.svelte.d.ts +4 -1
- package/dist/components/sidebar/SidebarItem.svelte +26 -32
- package/dist/components/sidebar/SidebarRoot.svelte +62 -0
- package/dist/components/sidebar/SidebarRoot.svelte.d.ts +14 -0
- package/dist/components/sidebar/index.d.ts +5 -1
- package/dist/components/sidebar/index.js +3 -1
- package/dist/components/skeleton/Skeleton.svelte +26 -15
- package/dist/components/slider/Slider.svelte +11 -10
- package/dist/components/spinner/Spinner.svelte +4 -11
- package/dist/components/status-indicator/StatusIndicator.svelte +1 -1
- package/dist/components/suggestions/SuggestionsItem.svelte +5 -5
- package/dist/components/switch/Switch.svelte +9 -14
- package/dist/components/tab-strip/TabStrip.svelte +340 -0
- package/dist/components/tab-strip/TabStrip.svelte.d.ts +31 -0
- package/dist/components/tabs/Tabs.svelte +13 -9
- package/dist/components/tabs/Tabs.svelte.d.ts +4 -0
- package/dist/components/text/Text.svelte +31 -23
- package/dist/components/text/Text.svelte.d.ts +8 -4
- package/dist/components/toast/Toaster.svelte +5 -5
- package/dist/components/tool/ToolContent.svelte +3 -8
- package/dist/components/tool/ToolSection.svelte +4 -5
- package/dist/components/tool/ToolTrigger.svelte +10 -9
- package/dist/components/tooltip/TooltipContent.svelte +3 -3
- package/dist/components/work-fold/WorkFold.svelte +5 -9
- package/dist/index.d.ts +16 -8
- package/dist/index.js +17 -10
- package/dist/internal/Code.svelte +3 -4
- package/dist/internal/Glyph.svelte +16 -0
- package/dist/internal/Glyph.svelte.d.ts +6 -0
- package/dist/internal/Tile.svelte +17 -37
- package/dist/internal/control.d.ts +4 -4
- package/dist/internal/control.js +20 -17
- package/dist/internal/layer.d.ts +3 -5
- package/dist/internal/layer.js +10 -12
- package/dist/internal/markers.stylex.d.ts +8 -0
- package/dist/internal/markers.stylex.js +4 -0
- package/dist/internal/plugin-order.d.ts +8 -0
- package/dist/internal/plugin-order.js +16 -0
- package/dist/internal/roving.js +5 -2
- package/dist/internal/shared.d.ts +8 -1
- package/dist/internal/shared.js +21 -5
- package/dist/internal/sx.d.ts +1 -1
- package/dist/internal/toggle.d.ts +1 -2
- package/dist/internal/toggle.js +8 -12
- package/dist/internal/visible.d.ts +32 -0
- package/dist/internal/visible.js +44 -0
- package/dist/markdown/ast.d.ts +209 -0
- package/dist/markdown/ast.js +10 -0
- package/dist/markdown/blocks.d.ts +177 -0
- package/dist/markdown/blocks.js +1489 -0
- package/dist/markdown/code-info.d.ts +10 -0
- package/dist/markdown/code-info.js +16 -0
- package/dist/markdown/common.d.ts +25 -0
- package/dist/markdown/common.js +107 -0
- package/dist/markdown/diagrams.d.ts +2 -0
- package/dist/markdown/diagrams.js +18 -0
- package/dist/markdown/file-paths.d.ts +29 -0
- package/dist/markdown/file-paths.js +80 -0
- package/dist/markdown/files.d.ts +16 -0
- package/dist/markdown/files.js +133 -0
- package/dist/markdown/highlight.d.ts +30 -0
- package/dist/markdown/highlight.js +127 -0
- package/dist/markdown/html.d.ts +23 -0
- package/dist/markdown/html.js +289 -0
- package/dist/markdown/images.d.ts +15 -0
- package/dist/markdown/images.js +45 -0
- package/dist/markdown/index.d.ts +12 -0
- package/dist/markdown/index.js +5 -0
- package/dist/markdown/inlines.d.ts +48 -0
- package/dist/markdown/inlines.js +1714 -0
- package/dist/markdown/math.d.ts +2 -0
- package/dist/markdown/math.js +22 -0
- package/dist/markdown/parse.d.ts +81 -0
- package/dist/markdown/parse.js +204 -0
- package/dist/markdown/plugin.d.ts +178 -0
- package/dist/markdown/plugin.js +160 -0
- package/dist/markdown/sanitize.d.ts +78 -0
- package/dist/markdown/sanitize.js +522 -0
- package/dist/markdown/share.d.ts +12 -0
- package/dist/markdown/share.js +59 -0
- package/dist/markdown/table.d.ts +13 -0
- package/dist/markdown/table.js +60 -0
- package/dist/preset.d.ts +40 -16
- package/dist/preset.js +169 -51
- package/dist/tokens.stylex.d.ts +55 -37
- package/dist/tokens.stylex.js +107 -62
- package/dist/typography.d.ts +48 -0
- package/dist/typography.js +55 -0
- package/dist/vite.d.ts +7 -1
- package/dist/vite.js +27 -2
- package/package.json +53 -6
- package/skills/building-ui/SKILL.md +242 -0
- package/skills/building-ui/references/components.md +207 -0
- package/skills/markdown/SKILL.md +452 -0
- package/skills/markdown/references/plugins.md +103 -0
- package/skills/setup/SKILL.md +191 -0
- package/skills/styling/SKILL.md +269 -0
- package/skills/theming/SKILL.md +184 -0
- package/dist/internal/loop.d.ts +0 -35
- package/dist/internal/loop.js +0 -72
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: setup
|
|
3
|
+
description: >
|
|
4
|
+
Use when adding @diegopluna/narvi to a SvelteKit or Vite app, or when Narvi components render
|
|
5
|
+
unstyled, styles are missing in dev or production, stylex.create throws at runtime, or Narvi is
|
|
6
|
+
used alongside Tailwind CSS 4.
|
|
7
|
+
metadata:
|
|
8
|
+
purpose: >
|
|
9
|
+
Install Narvi in a Svelte 5 app so its StyleX styles compile and load in development and in
|
|
10
|
+
production, with the base stylesheet in place, and order its cascade layer around Tailwind CSS
|
|
11
|
+
4 when the app uses it.
|
|
12
|
+
type: lifecycle
|
|
13
|
+
library: '@diegopluna/narvi'
|
|
14
|
+
library_version: '0.1.0'
|
|
15
|
+
sources:
|
|
16
|
+
- src/lib/vite.ts
|
|
17
|
+
- src/lib/base.css
|
|
18
|
+
- src/lib/components/dev-styles/DevStyles.svelte
|
|
19
|
+
- src/routes/docs/installation/+page.svelte
|
|
20
|
+
- src/routes/docs/styling/+page.svelte
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
# Set up Narvi
|
|
24
|
+
|
|
25
|
+
Narvi is a Svelte 5 component library styled with StyleX. Its styles compile to atomic CSS at
|
|
26
|
+
build time. Nothing styles at runtime, so **a component renders unstyled unless the Vite plugin
|
|
27
|
+
and the stylesheet wiring below are in place**. It needs Svelte 5.20 or later and Vite 6 or later,
|
|
28
|
+
and its colours use CSS `light-dark()` (Chrome 123, Safari 17.5, Firefox 120 and later).
|
|
29
|
+
|
|
30
|
+
Narvi is below 1.0, so a minor release can change an API. Pin the version you build against.
|
|
31
|
+
|
|
32
|
+
## Setup
|
|
33
|
+
|
|
34
|
+
1. Install the package, with StyleX for your own styles:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
pnpm add @diegopluna/narvi @stylexjs/stylex@0.19.1
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`@stylexjs/stylex` is only needed to import it in your own files. Keep it at the version Narvi
|
|
41
|
+
depends on (`npm view @diegopluna/narvi dependencies` lists it).
|
|
42
|
+
|
|
43
|
+
2. Add the plugin **after** `sveltekit()` (or after `svelte()` in a plain Vite app):
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// vite.config.ts
|
|
47
|
+
import { sveltekit } from '@sveltejs/kit/vite';
|
|
48
|
+
import { narvi } from '@diegopluna/narvi/vite';
|
|
49
|
+
import { defineConfig } from 'vite';
|
|
50
|
+
|
|
51
|
+
export default defineConfig({
|
|
52
|
+
plugins: [sveltekit(), narvi()]
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Leave the options `sveltekit()` is given as they are (SvelteKit 3 passes its adapter there).
|
|
57
|
+
The plugin also sets `build.cssTarget` to browsers that have `light-dark()` (Chrome and Edge
|
|
58
|
+
123, Firefox 120, Safari 17.5), so Vite does not rewrite it in the app's stylesheet. An app
|
|
59
|
+
that sets `build.cssTarget` itself keeps its own.
|
|
60
|
+
|
|
61
|
+
3. Load the styles. Mount `DevStyles` once in the root layout. It loads the dev server's
|
|
62
|
+
stylesheet and refreshes it on hot updates, and renders nothing in production. The root
|
|
63
|
+
layout must also import a stylesheet of your own, because in a production build the plugin
|
|
64
|
+
appends Narvi's CSS to a stylesheet the app already imports. A fresh SvelteKit app has no
|
|
65
|
+
`src/app.css`: create it.
|
|
66
|
+
|
|
67
|
+
```svelte
|
|
68
|
+
<!-- src/routes/+layout.svelte -->
|
|
69
|
+
<script lang="ts">
|
|
70
|
+
import '../app.css';
|
|
71
|
+
import { DevStyles } from '@diegopluna/narvi';
|
|
72
|
+
|
|
73
|
+
let { children } = $props();
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<DevStyles />
|
|
77
|
+
|
|
78
|
+
{@render children()}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```css
|
|
82
|
+
/* src/app.css */
|
|
83
|
+
@import '@diegopluna/narvi/base.css';
|
|
84
|
+
|
|
85
|
+
:root {
|
|
86
|
+
/* Narvi's interface text inherits the page's face. Set it on the root, not on a wrapper,
|
|
87
|
+
so menus and dialogs, which are placed at the end of the body, take it too. */
|
|
88
|
+
font-family:
|
|
89
|
+
ui-sans-serif,
|
|
90
|
+
system-ui,
|
|
91
|
+
-apple-system,
|
|
92
|
+
'Segoe UI',
|
|
93
|
+
sans-serif;
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
In a plain Vite app there is no layout: import the stylesheet in `src/main.ts` (Vite's
|
|
98
|
+
template already does) and mount `<DevStyles />` once in `src/App.svelte`.
|
|
99
|
+
|
|
100
|
+
`base.css` sets `color-scheme: light dark` (which Narvi's colours follow) and paints the root in
|
|
101
|
+
the `bg` and `text` tokens. It also adds a small reset: border-box sizing; no margin on the body,
|
|
102
|
+
headings, paragraphs and figures; lists without markers; and media as blocks. It sits in the
|
|
103
|
+
`narvi.base` layer, below every component, and any rule of yours outside a layer beats both.
|
|
104
|
+
|
|
105
|
+
4. Use a component:
|
|
106
|
+
|
|
107
|
+
```svelte
|
|
108
|
+
<script lang="ts">
|
|
109
|
+
import { Button } from '@diegopluna/narvi';
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<Button variant="primary" onclick={() => alert('Mellon!')}>Speak, friend</Button>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
In SvelteKit 3 apps there is no `$lib` alias by default. Aliases come from `package.json`
|
|
116
|
+
`imports` (often `#lib`), so the samples in these skills use relative imports such as
|
|
117
|
+
`'../lib/ember'`. Use the app's own alias if it has one.
|
|
118
|
+
|
|
119
|
+
Large headings use a serif (`Cormorant Garamond`), and code uses `Geist Mono`. Both fall back to
|
|
120
|
+
system faces. To load them, install `@fontsource/cormorant-garamond` (or the
|
|
121
|
+
`@fontsource-variable/` build) and `@fontsource/geist-mono` and import them in the root layout.
|
|
122
|
+
Narvi lists both naming styles, so neither needs a theme override.
|
|
123
|
+
|
|
124
|
+
## With Tailwind CSS 4
|
|
125
|
+
|
|
126
|
+
Out of the box, Narvi's layer comes after Tailwind's, so a utility such as `hidden` on a Button
|
|
127
|
+
changes nothing. Both of these changes are needed:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
// vite.config.ts
|
|
131
|
+
import { sveltekit } from '@sveltejs/kit/vite';
|
|
132
|
+
import tailwindcss from '@tailwindcss/vite';
|
|
133
|
+
import { narvi } from '@diegopluna/narvi/vite';
|
|
134
|
+
import { defineConfig } from 'vite';
|
|
135
|
+
|
|
136
|
+
export default defineConfig({
|
|
137
|
+
plugins: [tailwindcss(), sveltekit(), narvi({ tailwind: true })]
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```css
|
|
142
|
+
/* src/app.css: the order line comes first, before any @import */
|
|
143
|
+
@layer theme, base, narvi, components, utilities;
|
|
144
|
+
@import 'tailwindcss';
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The browser fixes the layer order from whichever stylesheet it reads first, and in development
|
|
148
|
+
that can be either one, so the plugin option and the CSS line are both needed. Narvi still beats
|
|
149
|
+
Preflight. Utilities replace whole properties: `p-0` on a Card removes its padding, not a part of it.
|
|
150
|
+
|
|
151
|
+
## Common mistakes
|
|
152
|
+
|
|
153
|
+
- **HIGH: `narvi()` listed before `sveltekit()`.** The plugin must receive components that the
|
|
154
|
+
Svelte compiler has already processed. Put it last. The build stops with "narvi() is listed
|
|
155
|
+
before the Svelte plugin"; in 0.1.0 the same mistake reads `Unexpected token` from
|
|
156
|
+
`@stylexjs/unplugin` in every `.svelte` file.
|
|
157
|
+
- **HIGH: the plugin missing from `vite.config.ts`.** The dev server fails with
|
|
158
|
+
`Failed to resolve import "virtual:stylex:runtime"`, or the page throws
|
|
159
|
+
`Unexpected 'stylex.defineVars' call at runtime`. Add `narvi()`.
|
|
160
|
+
- **HIGH: no stylesheet imported by the app.** In production, Narvi's CSS is appended to a
|
|
161
|
+
stylesheet the app imports. With none, the built site has unstyled components while `pnpm dev`
|
|
162
|
+
looks fine. Import `app.css` (or any CSS file) from the root layout.
|
|
163
|
+
- **HIGH: `DevStyles` missing.** In development, components render with class names but no
|
|
164
|
+
CSS. Mount it once in the root layout, not per page.
|
|
165
|
+
- **HIGH: adding Narvi to `optimizeDeps.include`.** Narvi ships StyleX source that the plugin
|
|
166
|
+
compiles. When Vite pre-bundles it, the page receives raw `stylex.create` calls that throw at
|
|
167
|
+
runtime. The plugin already keeps Narvi out of pre-bundling, so leave `optimizeDeps` alone. If
|
|
168
|
+
another package of yours ships StyleX source, pass it as `narvi({ externalPackages: ['pkg'] })`.
|
|
169
|
+
- **MEDIUM: lowering `build.cssTarget` below the browsers above.** Vite then rewrites
|
|
170
|
+
`light-dark()` in `base.css` into custom properties that follow only a `color-scheme` written in
|
|
171
|
+
a stylesheet. One set from script (`document.documentElement.style.colorScheme = 'dark'`)
|
|
172
|
+
darkens the components and leaves the page light, in production only. Leave the target to the
|
|
173
|
+
plugin, or switch schemes with a rule such as `:root[data-theme='dark'] { color-scheme: dark }`.
|
|
174
|
+
- **MEDIUM: putting the font on a layout `<div>`.** Popups are placed at the end of `<body>`,
|
|
175
|
+
outside that div, and fall back to the system font. Set `font-family` on `:root`.
|
|
176
|
+
- **MEDIUM: theming `bg` or `text` without updating the root.** `base.css` paints the root with
|
|
177
|
+
copies of those tokens. If you override them, set `:root { background-color; color }` to match.
|
|
178
|
+
|
|
179
|
+
## Completion
|
|
180
|
+
|
|
181
|
+
- `pnpm dev`: a `<Button variant="primary">` shows as a flat dark button (light scheme) with
|
|
182
|
+
rounded corners, not as a browser default button.
|
|
183
|
+
- `pnpm build`: the built CSS contains Narvi's layer. In a SvelteKit app, run
|
|
184
|
+
`grep -rl "@layer narvi" .svelte-kit/output/client` (in a plain Vite app,
|
|
185
|
+
`grep -rl "@layer narvi" dist`) and confirm it lists a file. If it doesn't, check step 3.
|
|
186
|
+
- pnpm may warn of an unmet peer, `@sveltejs/kit`, wanted by `runed` (a dependency of bits-ui) in
|
|
187
|
+
a SvelteKit 3 app. It can be ignored.
|
|
188
|
+
- With Tailwind, `<Button class="hidden">` disappears.
|
|
189
|
+
|
|
190
|
+
Next: to restyle components or write your own StyleX, load the `styling` skill. For dark mode and
|
|
191
|
+
custom themes, load `theming`. For building screens from Narvi's parts, load `building-ui`.
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: styling
|
|
3
|
+
description: >
|
|
4
|
+
Use when restyling Narvi components or writing your own styles in an app that uses
|
|
5
|
+
@diegopluna/narvi: the sx prop, StyleX with Narvi's design tokens and media conditions, hover
|
|
6
|
+
groups, styling elements or third-party components, plain CSS overrides, and building app
|
|
7
|
+
components the way Narvi does.
|
|
8
|
+
metadata:
|
|
9
|
+
purpose: >
|
|
10
|
+
Style Narvi components and an app's own components with StyleX and Narvi's tokens, so the
|
|
11
|
+
styles compile, follow the theme, win over Narvi's defaults predictably, and keep working with
|
|
12
|
+
hover, motion and forced-colour preferences.
|
|
13
|
+
type: core
|
|
14
|
+
library: '@diegopluna/narvi'
|
|
15
|
+
library_version: '0.1.0'
|
|
16
|
+
requires:
|
|
17
|
+
- setup
|
|
18
|
+
sources:
|
|
19
|
+
- src/lib/internal/sx.ts
|
|
20
|
+
- src/lib/tokens.stylex.ts
|
|
21
|
+
- src/lib/media.stylex.ts
|
|
22
|
+
- src/lib/index.ts
|
|
23
|
+
- src/routes/docs/styling/+page.svelte
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
# Style with Narvi
|
|
27
|
+
|
|
28
|
+
This assumes Narvi is installed and its styles load (see the `setup` skill). Prefer StyleX with
|
|
29
|
+
Narvi's tokens. Use plain CSS only for a one-off override or in an app that already uses Tailwind.
|
|
30
|
+
|
|
31
|
+
## Setup
|
|
32
|
+
|
|
33
|
+
Write styles in a `<script module>` block, so they are created once and not per instance. Take
|
|
34
|
+
values from the tokens and conditions from `media`:
|
|
35
|
+
|
|
36
|
+
```svelte
|
|
37
|
+
<script lang="ts" module>
|
|
38
|
+
import * as stylex from '@stylexjs/stylex';
|
|
39
|
+
import { colors, radius, space } from '@diegopluna/narvi/tokens.stylex.js';
|
|
40
|
+
|
|
41
|
+
const styles = stylex.create({
|
|
42
|
+
pill: { paddingInline: space.x4, borderRadius: radius.pill },
|
|
43
|
+
quiet: { backgroundColor: colors.fillSunken, color: colors.accent }
|
|
44
|
+
});
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<script lang="ts">
|
|
48
|
+
import { Button } from '@diegopluna/narvi';
|
|
49
|
+
let { quiet = false } = $props();
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<Button sx={[styles.pill, quiet && styles.quiet]}>Pill</Button>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Every Narvi component takes `sx`: one style, or a nested array of styles. `false`, `null` and
|
|
56
|
+
`undefined` are skipped. These styles merge after the component's own, so yours win one property
|
|
57
|
+
at a time. Every component also passes `class` through to its root element.
|
|
58
|
+
|
|
59
|
+
## Tokens
|
|
60
|
+
|
|
61
|
+
Import tokens from `@diegopluna/narvi/tokens.stylex.js`, and always from that exact path.
|
|
62
|
+
StyleX resolves them while it compiles, so a re-export from a file of your own (or from the
|
|
63
|
+
package root) breaks them.
|
|
64
|
+
|
|
65
|
+
- `colors`, named by family. Backgrounds, which are opaque: `bg`, `bgRaised`, `bgWindow` (behind
|
|
66
|
+
an app shell's sheet). Fills, which are translucent and go over any background: `fillHover`,
|
|
67
|
+
`fillSelected` (what is chosen or open now), `fillMuted` (a flat block) and `fillSunken` (a
|
|
68
|
+
well: code, a track, a tray). Borders: `border`, `borderStrong`,
|
|
69
|
+
`borderControl` (the boundary of a control, 3:1), `borderControlHover`. Shade: `shadow` (the
|
|
70
|
+
one drop, under what floats over the page) and `scrim` (behind a dialog). Text: `text`,
|
|
71
|
+
`textMuted`, `textSubtle`. The accent, which marks state: `accent`, `accentVivid`, `onAccent`,
|
|
72
|
+
`accentSoft`, `focusRing`, `focusHalo`. Status: `success`, `warning`, `danger`, and their
|
|
73
|
+
`…Soft` (fills) and `…Text` (text on a soft fill) variants. Syntax: `codeKey`, `codeString`,
|
|
74
|
+
`codeNumber`, `codeLiteral`, `codePunct`. The primary button: `primary`, `onPrimary`. The
|
|
75
|
+
thumb of a switch or slider: `thumb`.
|
|
76
|
+
- `space`: a 4px scale from `x1` (4px) to `x16` (64px).
|
|
77
|
+
- `radius`, named for what takes it: `chip` (a key cap, a checkbox), `item` (a small control),
|
|
78
|
+
`control` (a button, a field, a row), `panel` (a notice, a sheet), `card` (a card, a popover),
|
|
79
|
+
`frame` and `pill`.
|
|
80
|
+
- `font`: `body` (inherits the page), `display` (the heading serif), `mono`; the weights
|
|
81
|
+
`weightRegular`, `weightMedium` and `weightSemibold`; the line heights `leadingTight`,
|
|
82
|
+
`leadingSnug` and `leadingNormal`; and sizes named for what they set: `sizeMicro` (11px),
|
|
83
|
+
`sizeCaption` (12), `sizeMeta` (13), `sizeBody` (14), `sizeReading` (16) and `sizeTitle` (17).
|
|
84
|
+
Prefer a whole role to a size: `import { typography } from '@diegopluna/narvi'` gives `title`,
|
|
85
|
+
`reading`, `label`, `body`, `meta`, `caption` and `micro`, each a size, a weight and a line
|
|
86
|
+
height, for `stylex.attrs(typography.caption, styles.count)`.
|
|
87
|
+
- `play.whileVisible`: the `animationPlayState` for an animation that never ends. Attach
|
|
88
|
+
`whileVisible` (from the package root) to its element and it runs only while it can be seen.
|
|
89
|
+
Step such an animation (`steps(30)` over two seconds) so it costs a few frames a second.
|
|
90
|
+
- `motion`: `fast` 120ms, `base` 150ms, `slow` 200ms; `easeOut` for feedback, `easeInOut` for
|
|
91
|
+
things that travel. Nothing in an interface takes longer than `motion.slow`.
|
|
92
|
+
- `pageMotion`: a slower scale for pages read as they scroll (`Reveal` uses it). Never use it
|
|
93
|
+
for controls.
|
|
94
|
+
|
|
95
|
+
Each colour holds a light and a dark value, so a style that uses tokens follows the scheme. A
|
|
96
|
+
hex value does not.
|
|
97
|
+
|
|
98
|
+
## Conditions
|
|
99
|
+
|
|
100
|
+
Use `media` from `@diegopluna/narvi/media.stylex.js` as keys inside `stylex.create`:
|
|
101
|
+
|
|
102
|
+
```svelte
|
|
103
|
+
<script lang="ts" module>
|
|
104
|
+
import * as stylex from '@stylexjs/stylex';
|
|
105
|
+
import { media } from '@diegopluna/narvi/media.stylex.js';
|
|
106
|
+
import { colors, motion, space } from '@diegopluna/narvi/tokens.stylex.js';
|
|
107
|
+
|
|
108
|
+
const styles = stylex.create({
|
|
109
|
+
nav: {
|
|
110
|
+
display: { default: 'none', [media.md]: 'flex' },
|
|
111
|
+
gap: { default: space.x4, [media.lg]: space.x8 }
|
|
112
|
+
},
|
|
113
|
+
link: {
|
|
114
|
+
color: { default: colors.textMuted, ':hover': { default: null, [media.hover]: colors.text } },
|
|
115
|
+
transitionProperty: 'color',
|
|
116
|
+
transitionDuration: { default: '0s', [media.motion]: motion.fast }
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
</script>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- `media.hover`: a precise pointer that can hover. Put every hover style behind it, so a tap never
|
|
123
|
+
leaves one stuck.
|
|
124
|
+
- `media.motion` and `media.reducedMotion`: put movement behind `motion`.
|
|
125
|
+
- `media.dark`, `media.forcedColors` and `media.coarse` (touch).
|
|
126
|
+
- `media.sm`, `md`, `lg` and `xl`: minimum widths of 640, 768, 1024 and 1280px, mobile first.
|
|
127
|
+
|
|
128
|
+
For layout, the `Stack` and `Grid` components take the same breakpoints in their props
|
|
129
|
+
(`gap={{ default: 2, md: 4 }}`), so you often don't need a style at all.
|
|
130
|
+
|
|
131
|
+
## Core patterns
|
|
132
|
+
|
|
133
|
+
### Style an element, or a component that takes only a class
|
|
134
|
+
|
|
135
|
+
`sx()` resolves styles to `class` and `style` attributes to spread. `classes()` resolves them to
|
|
136
|
+
one class string. Both are exported from `@diegopluna/narvi`:
|
|
137
|
+
|
|
138
|
+
```svelte
|
|
139
|
+
<script lang="ts">
|
|
140
|
+
import { classes, sx } from '@diegopluna/narvi';
|
|
141
|
+
import Logo from 'some-icon-library/Logo.svelte';
|
|
142
|
+
</script>
|
|
143
|
+
|
|
144
|
+
<svg {...sx(styles.mark, active && styles.lit)}>…</svg>
|
|
145
|
+
<Logo class={classes(styles.mark)} />
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`classes()` drops the inline `style` that dynamic styles (`stylex.create` functions) set, so use
|
|
149
|
+
`sx()` for those.
|
|
150
|
+
|
|
151
|
+
### Hover groups
|
|
152
|
+
|
|
153
|
+
Mark the ancestor with `stylex.defaultMarker()` and style the descendant with
|
|
154
|
+
`stylex.when.ancestor(':hover')`. A component's `sx` takes markers beside styles:
|
|
155
|
+
|
|
156
|
+
```svelte
|
|
157
|
+
<script lang="ts" module>
|
|
158
|
+
import * as stylex from '@stylexjs/stylex';
|
|
159
|
+
import { media } from '@diegopluna/narvi/media.stylex.js';
|
|
160
|
+
import { colors } from '@diegopluna/narvi/tokens.stylex.js';
|
|
161
|
+
|
|
162
|
+
const styles = stylex.create({
|
|
163
|
+
arrow: {
|
|
164
|
+
color: {
|
|
165
|
+
default: colors.textMuted,
|
|
166
|
+
[stylex.when.ancestor(':hover')]: { default: null, [media.hover]: colors.accent }
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
</script>
|
|
171
|
+
|
|
172
|
+
<script lang="ts">
|
|
173
|
+
import { Card, sx } from '@diegopluna/narvi';
|
|
174
|
+
</script>
|
|
175
|
+
|
|
176
|
+
<Card href="/download" variant="outlined" sx={stylex.defaultMarker()}>
|
|
177
|
+
Download <span aria-hidden="true" {...sx(styles.arrow)}>→</span>
|
|
178
|
+
</Card>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
`when.ancestor` matches any marked ancestor. For a group nested in another, use your own
|
|
182
|
+
`stylex.defineMarker()`, exported from a `.stylex.ts` file.
|
|
183
|
+
|
|
184
|
+
### Give tokens other names
|
|
185
|
+
|
|
186
|
+
A style can set custom properties, which is useful when another library reads its own variable names:
|
|
187
|
+
|
|
188
|
+
```svelte
|
|
189
|
+
<script lang="ts" module>
|
|
190
|
+
import * as stylex from '@stylexjs/stylex';
|
|
191
|
+
import { colors } from '@diegopluna/narvi/tokens.stylex.js';
|
|
192
|
+
const styles = stylex.create({
|
|
193
|
+
aliases: { '--primary': colors.accent, '--background': colors.bg }
|
|
194
|
+
});
|
|
195
|
+
</script>
|
|
196
|
+
|
|
197
|
+
<script lang="ts">
|
|
198
|
+
import { Theme } from '@diegopluna/narvi';
|
|
199
|
+
let { children } = $props();
|
|
200
|
+
</script>
|
|
201
|
+
|
|
202
|
+
<Theme sx={styles.aliases}>{@render children()}</Theme>
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Plain CSS
|
|
206
|
+
|
|
207
|
+
All of Narvi's CSS is in one cascade layer, `narvi`. Any rule outside a layer beats it,
|
|
208
|
+
whatever its specificity, so a single class works: `<Button class="sunset">` with
|
|
209
|
+
`:global(.sunset) { … }`. For Tailwind, see the `setup` skill.
|
|
210
|
+
|
|
211
|
+
### Your own components
|
|
212
|
+
|
|
213
|
+
Build them the way Narvi does: styles in the module script, applied with `stylex.attrs` (or
|
|
214
|
+
`sx()`). Take `sx` and `class` props and merge them last, so callers can restyle your component too:
|
|
215
|
+
|
|
216
|
+
```svelte
|
|
217
|
+
<!-- Panel.svelte -->
|
|
218
|
+
<script lang="ts" module>
|
|
219
|
+
import * as stylex from '@stylexjs/stylex';
|
|
220
|
+
import type { Snippet } from 'svelte';
|
|
221
|
+
import type { Sx } from '@diegopluna/narvi';
|
|
222
|
+
import { colors, radius, space } from '@diegopluna/narvi/tokens.stylex.js';
|
|
223
|
+
|
|
224
|
+
export type PanelProps = { sx?: Sx; class?: string; children: Snippet };
|
|
225
|
+
|
|
226
|
+
const styles = stylex.create({
|
|
227
|
+
panel: {
|
|
228
|
+
padding: space.x4,
|
|
229
|
+
borderRadius: radius.panel,
|
|
230
|
+
backgroundColor: colors.bgRaised,
|
|
231
|
+
color: colors.text
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
</script>
|
|
235
|
+
|
|
236
|
+
<script lang="ts">
|
|
237
|
+
import { sx as toAttrs } from '@diegopluna/narvi';
|
|
238
|
+
|
|
239
|
+
let { sx, class: className, children }: PanelProps = $props();
|
|
240
|
+
const attrs = $derived(toAttrs(styles.panel, sx));
|
|
241
|
+
</script>
|
|
242
|
+
|
|
243
|
+
<div class={[attrs.class, className]} style={attrs.style}>{@render children()}</div>
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Common mistakes
|
|
247
|
+
|
|
248
|
+
- **HIGH: tokens imported through a re-export.** `import { colors } from '../lib/theme'` (a file
|
|
249
|
+
that re-exports them) fails the build with "Could not resolve the path to the imported file", and
|
|
250
|
+
the package root doesn't export them at all. Import from
|
|
251
|
+
`@diegopluna/narvi/tokens.stylex.js` and `@diegopluna/narvi/media.stylex.js` directly.
|
|
252
|
+
- **HIGH: `defineVars`, `defineConsts` or `defineMarker` outside a `.stylex.ts` file.** StyleX
|
|
253
|
+
requires these in files named `*.stylex.ts` (or `.js`), exported by name.
|
|
254
|
+
- **MEDIUM: styles created in the instance `<script>`.** It compiles, but the styles are
|
|
255
|
+
created again for every instance. Put `stylex.create` in a `<script module>` block.
|
|
256
|
+
- **MEDIUM: a raw `@media` string mixed with `media` constants on one property.** StyleX ranks a
|
|
257
|
+
constant above a written-out query, so the cascade order goes wrong. Use the constants throughout.
|
|
258
|
+
- **MEDIUM: hover without `media.hover`, or a transition without `media.motion`.** The hover
|
|
259
|
+
sticks after a tap, and the motion ignores reduced-motion settings.
|
|
260
|
+
- **MEDIUM: drawing an edge with `border` on a Narvi surface.** Narvi draws edges with
|
|
261
|
+
`box-shadow` (`inset 0 0 0 1px colors.border`). A border changes the size and doesn't match.
|
|
262
|
+
For something new that must stay visible in forced-colours mode, add a transparent `outline`
|
|
263
|
+
of 1px, which turns into the edge there.
|
|
264
|
+
- **MEDIUM: a Tailwind utility seen as "adjusting".** Utilities replace the whole property.
|
|
265
|
+
|
|
266
|
+
## Completion
|
|
267
|
+
|
|
268
|
+
The styled element shows the new value in the browser's computed styles, in both the light and
|
|
269
|
+
the dark scheme. A hover style does not apply on a touch emulator, and `pnpm build` passes.
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: theming
|
|
3
|
+
description: >
|
|
4
|
+
Use when changing light/dark mode, adding a theme toggle, scoping a colour scheme to part of a
|
|
5
|
+
page, rebranding Narvi's accent with a custom theme, or reading a Narvi colour token in a
|
|
6
|
+
canvas, WebGL or chart library, in an app that uses @diegopluna/narvi.
|
|
7
|
+
metadata:
|
|
8
|
+
purpose: >
|
|
9
|
+
Control Narvi's light and dark schemes, override its tokens with custom themes that also reach
|
|
10
|
+
menus and dialogs, and resolve token colours for drawing code that cannot read CSS variables.
|
|
11
|
+
type: core
|
|
12
|
+
library: '@diegopluna/narvi'
|
|
13
|
+
library_version: '0.1.0'
|
|
14
|
+
requires:
|
|
15
|
+
- setup
|
|
16
|
+
sources:
|
|
17
|
+
- src/lib/components/theme/Theme.svelte
|
|
18
|
+
- src/lib/tokens.stylex.ts
|
|
19
|
+
- src/lib/color.ts
|
|
20
|
+
- src/lib/base.css
|
|
21
|
+
- src/routes/docs/theming/+page.svelte
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Theme Narvi
|
|
25
|
+
|
|
26
|
+
Every colour token holds both halves as `light-dark(light, dark)`. Which half shows depends on
|
|
27
|
+
the `color-scheme` of the element, so **the scheme is a single CSS property**, and a custom theme
|
|
28
|
+
is a set of token overrides. This assumes the `setup` skill is done: `base.css` sets
|
|
29
|
+
`color-scheme: light dark` on the root, which follows the OS.
|
|
30
|
+
|
|
31
|
+
## Core patterns
|
|
32
|
+
|
|
33
|
+
### Let people choose light or dark
|
|
34
|
+
|
|
35
|
+
Set the scheme on the root, with an attribute you control:
|
|
36
|
+
|
|
37
|
+
```css
|
|
38
|
+
/* src/app.css, after the base import */
|
|
39
|
+
:root[data-theme='light'] {
|
|
40
|
+
color-scheme: light;
|
|
41
|
+
}
|
|
42
|
+
:root[data-theme='dark'] {
|
|
43
|
+
color-scheme: dark;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```svelte
|
|
48
|
+
<script lang="ts">
|
|
49
|
+
import { SegmentedControl } from '@diegopluna/narvi';
|
|
50
|
+
|
|
51
|
+
let theme = $state('system');
|
|
52
|
+
|
|
53
|
+
$effect(() => {
|
|
54
|
+
if (theme === 'system') delete document.documentElement.dataset.theme;
|
|
55
|
+
else document.documentElement.dataset.theme = theme;
|
|
56
|
+
});
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<SegmentedControl
|
|
60
|
+
aria-label="Theme"
|
|
61
|
+
bind:value={theme}
|
|
62
|
+
options={[
|
|
63
|
+
{ value: 'system', label: 'System' },
|
|
64
|
+
{ value: 'light', label: 'Light' },
|
|
65
|
+
{ value: 'dark', label: 'Dark' }
|
|
66
|
+
]}
|
|
67
|
+
/>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To store the choice, save it (for example in `localStorage`) and set `data-theme` in a small
|
|
71
|
+
inline script in `app.html` before the body renders. That avoids a flash of the other scheme.
|
|
72
|
+
|
|
73
|
+
### Force a scheme on part of the page
|
|
74
|
+
|
|
75
|
+
```svelte
|
|
76
|
+
<Theme scheme="dark">…</Theme>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`scheme` takes `'light' | 'dark' | 'system'`. The wrapper uses `display: contents`, so it adds
|
|
80
|
+
no box to the layout.
|
|
81
|
+
|
|
82
|
+
### Rebrand the accent
|
|
83
|
+
|
|
84
|
+
This needs `@stylexjs/stylex` in the app's dependencies (step 1 of the `setup` skill).
|
|
85
|
+
|
|
86
|
+
Narvi keeps colour for state: `accent` (blue by default) marks focus, selection and progress.
|
|
87
|
+
Override those tokens together with `stylex.createTheme`, writing each as `light-dark()`:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
// src/lib/ember.ts
|
|
91
|
+
import * as stylex from '@stylexjs/stylex';
|
|
92
|
+
import { colors } from '@diegopluna/narvi/tokens.stylex.js';
|
|
93
|
+
|
|
94
|
+
export const ember = stylex.createTheme(colors, {
|
|
95
|
+
accent: 'light-dark(#ab4b00, #f18d57)',
|
|
96
|
+
accentVivid: 'light-dark(#c15b18, #e66e20)',
|
|
97
|
+
onAccent: 'light-dark(#ffffff, #170d08)',
|
|
98
|
+
accentSoft: 'light-dark(rgb(171 75 0 / 0.1), rgb(241 141 87 / 0.14))',
|
|
99
|
+
focusRing: 'light-dark(#ab4b00, #f18d57)',
|
|
100
|
+
focusHalo: 'light-dark(rgb(171 75 0 / 0.2), rgb(241 141 87 / 0.26))'
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**For the whole app, put the theme's class on `<html>`**, so it also reaches menus, popovers,
|
|
105
|
+
selects, tooltips, dialogs and toasts:
|
|
106
|
+
|
|
107
|
+
```svelte
|
|
108
|
+
<!-- src/routes/+layout.svelte -->
|
|
109
|
+
<script lang="ts">
|
|
110
|
+
import * as stylex from '@stylexjs/stylex';
|
|
111
|
+
import { ember } from '../lib/ember';
|
|
112
|
+
|
|
113
|
+
let { children } = $props();
|
|
114
|
+
|
|
115
|
+
$effect(() => {
|
|
116
|
+
const names = (stylex.attrs(ember).class ?? '').split(' ').filter(Boolean);
|
|
117
|
+
document.documentElement.classList.add(...names);
|
|
118
|
+
return () => document.documentElement.classList.remove(...names);
|
|
119
|
+
});
|
|
120
|
+
</script>
|
|
121
|
+
|
|
122
|
+
{@render children()}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The class is added once the page hydrates. With server rendering or prerendering, the first
|
|
126
|
+
paint shows the default accent for a moment. There's no verified way yet to put the class into the
|
|
127
|
+
server-rendered `<html>`. In a client-only app (`ssr = false`, as in Electron), there is no flash.
|
|
128
|
+
|
|
129
|
+
For a section that has no popups, `<Theme theme={ember}>…</Theme>` is enough. It also takes an
|
|
130
|
+
array of themes. The same `createTheme` works for `space`, `radius`, `font` and `motion`;
|
|
131
|
+
overriding `font.display` changes the heading serif.
|
|
132
|
+
|
|
133
|
+
### Paint a token in a canvas, WebGL or a chart
|
|
134
|
+
|
|
135
|
+
A token is a CSS variable that holds a `light-dark()` pair, which drawing code cannot read. Ask
|
|
136
|
+
the browser instead:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { resolveColor, resolveRgba } from '@diegopluna/narvi';
|
|
140
|
+
import { colors } from '@diegopluna/narvi/tokens.stylex.js';
|
|
141
|
+
|
|
142
|
+
export function draw(canvas: HTMLCanvasElement) {
|
|
143
|
+
const context = canvas.getContext('2d');
|
|
144
|
+
if (!context) return;
|
|
145
|
+
// A colour string for the scheme and theme in force at `scope`
|
|
146
|
+
context.fillStyle = resolveColor(colors.accent, { scope: canvas });
|
|
147
|
+
// Or channels, for WebGL
|
|
148
|
+
const [r, g, b, alpha] = resolveRgba(colors.text, { scope: canvas });
|
|
149
|
+
return [r, g, b, alpha];
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Call them in the browser only, from an effect or an event, never while rendering on the server.
|
|
154
|
+
Pass the element you draw in as `scope`, so a dark `Theme` around it counts. Draw again when the
|
|
155
|
+
scheme changes (`matchMedia('(prefers-color-scheme: dark)')`, or your toggle).
|
|
156
|
+
|
|
157
|
+
## Common mistakes
|
|
158
|
+
|
|
159
|
+
- **HIGH: popups inside a `<Theme>` lose it.** Menus, popovers, selects, tooltips, dialogs and
|
|
160
|
+
sheets render at the end of `<body>`, outside the Theme's element, so they take the root's
|
|
161
|
+
scheme and tokens. This was checked in Chromium against 0.1.0: a Popover opened inside
|
|
162
|
+
`<Theme theme={…} scheme="dark">` painted the default accent in the `normal` scheme, while text
|
|
163
|
+
in normal flow took the theme. For an app-wide theme or scheme, set it on `<html>` as shown above.
|
|
164
|
+
- **HIGH: hex values in a theme.** A theme value without `light-dark()` loses the other scheme.
|
|
165
|
+
- **MEDIUM: overriding `accent` alone.** Focus rings and soft fills use `focusRing`, `accentSoft`
|
|
166
|
+
and `focusHalo`, so override the whole set or the accent looks patched.
|
|
167
|
+
- **MEDIUM: overriding `text` alone.** `fillHover`, `fillSelected`, `fillMuted`, `fillSunken`,
|
|
168
|
+
`border`, `borderStrong`, `shadow` and `scrim` are the text colour at an opacity, and
|
|
169
|
+
`bgWindow` is `bg` a shade deeper. Change the neutrals and these keep
|
|
170
|
+
the old tint unless they are overridden as well. A preset from narvi.dpeter.dev/create does it.
|
|
171
|
+
- **MEDIUM: using the accent for decoration.** `accent` means "state": focus, selection, progress,
|
|
172
|
+
something live. Status uses `success`, `warning` and `danger`. Painting headings or icons in
|
|
173
|
+
`accent` makes the state colour stop meaning anything.
|
|
174
|
+
- **MEDIUM: overriding `bg` or `text` without the root.** `base.css` paints `:root` with copies
|
|
175
|
+
of those two. Set the root's `background-color` and `color` to match your override.
|
|
176
|
+
- **MEDIUM: `resolveColor` during SSR.** It needs the DOM, so it throws or returns nothing on the
|
|
177
|
+
server.
|
|
178
|
+
|
|
179
|
+
## Completion
|
|
180
|
+
|
|
181
|
+
- Switching `data-theme` (or the OS setting) flips every Narvi surface, including an open menu.
|
|
182
|
+
- With a brand theme on `<html>`, a focused Button's ring and an open Select's highlighted item
|
|
183
|
+
use the new accent.
|
|
184
|
+
- A canvas colour matches the CSS one in both schemes.
|
package/dist/internal/loop.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One clock for everything that moves for as long as it is on screen: spinners,
|
|
3
|
-
* shimmers, the bar of an unknown amount.
|
|
4
|
-
*
|
|
5
|
-
* A CSS animation that never ends repaints at the display's rate, 120 times a
|
|
6
|
-
* second on a ProMotion screen, and restyles its element every frame. In Eregion
|
|
7
|
-
* one shimmer held the renderer at 19% of a core and the GPU at 15%. So these
|
|
8
|
-
* loops share a single timer instead: 20 frames a second while the window is in
|
|
9
|
-
* front, 5 while another app is, and none at all while the window is hidden.
|
|
10
|
-
* Indicators read perfectly well at that rate.
|
|
11
|
-
*/
|
|
12
|
-
import type { Attachment } from 'svelte/attachments';
|
|
13
|
-
type Tick = (now: number) => void;
|
|
14
|
-
/** Calls `tick` on the shared clock until the returned function is called. */
|
|
15
|
-
export declare function onLoop(tick: Tick): () => void;
|
|
16
|
-
export type LoopFrame = {
|
|
17
|
-
/** How far through the cycle, from 0 to 1. */
|
|
18
|
-
progress: number;
|
|
19
|
-
/** True when the person has asked for less motion. */
|
|
20
|
-
reduced: boolean;
|
|
21
|
-
/** True when the element reads right to left, worked out once when it starts. */
|
|
22
|
-
rtl: boolean;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* An attachment that runs `frame` on the shared clock while the element is on
|
|
26
|
-
* the page. `period` is one cycle in milliseconds, or a function of whether the
|
|
27
|
-
* person has asked for less motion. `reset` undoes what `frame` set, when the
|
|
28
|
-
* loop stops; by default it clears the inline styles `frame` wrote.
|
|
29
|
-
*
|
|
30
|
-
* ```svelte
|
|
31
|
-
* <span {@attach loop(1000, (el, { progress }) => (el.style.rotate = `${progress}turn`))}></span>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export declare function loop(period: number | ((reduced: boolean) => number), frame: (element: HTMLElement | SVGElement, state: LoopFrame) => void, reset?: (element: HTMLElement | SVGElement) => void): Attachment<HTMLElement | SVGElement>;
|
|
35
|
-
export {};
|