@stacksjs/components 0.2.347 → 0.2.349
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/dist/Calendar-f3nzpc2y.stx +320 -0
- package/dist/DateRangePicker-7vc2rvc2.stx +218 -0
- package/dist/EmptyState-s790dbym.stx +57 -0
- package/dist/ThemeToggle-4wm9zare.stx +155 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -9
- package/dist/ui/date-range-picker/index.d.ts +22 -0
- package/dist/ui/empty-state/index.d.ts +10 -0
- package/dist/ui/theme-toggle/index.d.ts +10 -0
- package/dist/utils/highlighter.js +7 -1
- package/package.json +3 -3
- package/src/index.ts +3 -0
- package/src/ui/calendar/Calendar.stx +146 -15
- package/src/ui/date-range-picker/DateRangePicker.stx +218 -0
- package/src/ui/date-range-picker/index.ts +26 -0
- package/src/ui/empty-state/EmptyState.stx +57 -0
- package/src/ui/empty-state/index.ts +13 -0
- package/src/ui/theme-toggle/ThemeToggle.stx +155 -0
- package/src/ui/theme-toggle/index.ts +13 -0
- package/src/utils/highlighter.ts +155 -11
- package/dist/Calendar-ppqdw19s.stx +0 -189
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* The control four of six apps hand-rolled (stacksjs/stx#1981).
|
|
4
|
+
*
|
|
5
|
+
* `<Sidebar>` already has `followSystemAppearance` and a `theme` prop, so the
|
|
6
|
+
* library had a theme concept and no standalone control to drive it. Every
|
|
7
|
+
* hand-rolled version is the same three states, the same persistence, and the
|
|
8
|
+
* same pre-paint flash to avoid.
|
|
9
|
+
*
|
|
10
|
+
* That last part is why it belongs here, and it is also the part this component
|
|
11
|
+
* does NOT reimplement: `useColorMode()` reads the options the pre-paint boot
|
|
12
|
+
* script published on `window.__STX_COLOR_MODE__` (stacksjs/stx#1794), so the
|
|
13
|
+
* class already on <html> before first paint and the storage key this reads are
|
|
14
|
+
* the same ones. A toggle that picked its own key would undo the theme on
|
|
15
|
+
* hydration - which is the flash, arriving later instead of sooner.
|
|
16
|
+
*/
|
|
17
|
+
export const variant = $props.variant === 'button' ? 'button' : 'group'
|
|
18
|
+
export const className = $props.className || ''
|
|
19
|
+
export const lightLabel = $props.lightLabel || 'Light'
|
|
20
|
+
export const darkLabel = $props.darkLabel || 'Dark'
|
|
21
|
+
export const systemLabel = $props.systemLabel || 'System'
|
|
22
|
+
/** Labels are read by assistive tech in `button` variant, where only an icon shows. */
|
|
23
|
+
export const showLabels = $bool($props.showLabels, variant === 'group')
|
|
24
|
+
|
|
25
|
+
export const groupClasses = `inline-flex items-center gap-1 p-1 bg-surface-sunken rounded-control ${className}`.trim()
|
|
26
|
+
export const buttonClasses = `inline-flex items-center justify-center gap-2 h-9 w-9 text-fg-soft rounded-control hover:bg-surface-hover hover:text-fg-strong focus:outline-none focus:ring-2 focus:ring-accent focus:ring-offset-2 focus:ring-offset-panel transition-colors ${className}`.trim()
|
|
27
|
+
export const optionClasses = 'inline-flex items-center justify-center gap-1.5 px-2.5 py-1.5 text-sm text-fg-soft rounded-control hover:text-fg-strong focus:outline-none focus:ring-2 focus:ring-accent transition-colors'
|
|
28
|
+
export const optionActiveClasses = 'inline-flex items-center justify-center gap-1.5 px-2.5 py-1.5 text-sm font-medium text-fg bg-panel shadow-sm rounded-control focus:outline-none focus:ring-2 focus:ring-accent transition-colors'
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<script client>
|
|
32
|
+
const emit = defineEmits()
|
|
33
|
+
const variant = {{ variant }}
|
|
34
|
+
const labels = { light: {{ lightLabel }}, dark: {{ darkLabel }}, system: {{ systemLabel }} }
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* `useColorMode()` owns the class on <html>, the storage key and the system
|
|
38
|
+
* media query. This component owns none of that - it is a control, and the
|
|
39
|
+
* preference below is a mirror so the template can react to it.
|
|
40
|
+
*/
|
|
41
|
+
const colorMode = useColorMode()
|
|
42
|
+
const preference = state(colorMode.preference)
|
|
43
|
+
const resolved = state(colorMode.mode)
|
|
44
|
+
|
|
45
|
+
let unsubscribe = null
|
|
46
|
+
|
|
47
|
+
onMount(() => {
|
|
48
|
+
// Another tab, another toggle, or the OS changing under an `auto` preference.
|
|
49
|
+
// Without this the control shows a stale answer while the page is correct.
|
|
50
|
+
unsubscribe = colorMode.subscribe((mode, pref) => {
|
|
51
|
+
resolved.set(mode)
|
|
52
|
+
preference.set(pref)
|
|
53
|
+
})
|
|
54
|
+
// The boot script may have resolved a preference after this scope captured
|
|
55
|
+
// its initial value, so take it again on mount.
|
|
56
|
+
preference.set(colorMode.preference)
|
|
57
|
+
resolved.set(colorMode.mode)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
onDestroy(() => {
|
|
61
|
+
if (unsubscribe) unsubscribe()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
function choose(next) {
|
|
65
|
+
colorMode.set(next)
|
|
66
|
+
preference.set(next === 'system' ? 'auto' : next)
|
|
67
|
+
resolved.set(colorMode.mode)
|
|
68
|
+
emit('change', { preference: preference(), mode: resolved() })
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/*
|
|
72
|
+
* The cycle for the single-button variant: light to dark to system and round.
|
|
73
|
+
* `auto` is the stored spelling and `system` the one people say, and
|
|
74
|
+
* `useColorMode().set` accepts either.
|
|
75
|
+
*/
|
|
76
|
+
function cycle() {
|
|
77
|
+
const order = ['light', 'dark', 'auto']
|
|
78
|
+
const at = order.indexOf(preference())
|
|
79
|
+
choose(order[(at + 1) % order.length])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isActive(option) {
|
|
83
|
+
const pref = preference()
|
|
84
|
+
return option === 'system' ? pref === 'auto' : pref === option
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function optionButtonClasses(option) {
|
|
88
|
+
return isActive(option) ? {{ optionActiveClasses }} : {{ optionClasses }}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** What the single button says it will do, for a screen reader. */
|
|
92
|
+
const triggerLabel = derived(() => {
|
|
93
|
+
const pref = preference()
|
|
94
|
+
const name = pref === 'auto' ? labels.system : labels[pref]
|
|
95
|
+
return 'Theme: ' + name
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
defineExpose({ preference, resolved, choose, cycle, isActive, triggerLabel })
|
|
99
|
+
</script>
|
|
100
|
+
|
|
101
|
+
@if(variant === 'group')
|
|
102
|
+
<div class="{{ groupClasses }}" role="group" aria-label="Colour theme">
|
|
103
|
+
<button
|
|
104
|
+
type="button"
|
|
105
|
+
:class="optionButtonClasses('light')"
|
|
106
|
+
:aria-pressed="isActive('light') ? 'true' : 'false'"
|
|
107
|
+
@click="choose('light')"
|
|
108
|
+
>
|
|
109
|
+
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
110
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
|
|
111
|
+
</svg>
|
|
112
|
+
@if(showLabels)<span>{{ lightLabel }}</span>@endif
|
|
113
|
+
</button>
|
|
114
|
+
<button
|
|
115
|
+
type="button"
|
|
116
|
+
:class="optionButtonClasses('dark')"
|
|
117
|
+
:aria-pressed="isActive('dark') ? 'true' : 'false'"
|
|
118
|
+
@click="choose('dark')"
|
|
119
|
+
>
|
|
120
|
+
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
121
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
|
|
122
|
+
</svg>
|
|
123
|
+
@if(showLabels)<span>{{ darkLabel }}</span>@endif
|
|
124
|
+
</button>
|
|
125
|
+
<button
|
|
126
|
+
type="button"
|
|
127
|
+
:class="optionButtonClasses('system')"
|
|
128
|
+
:aria-pressed="isActive('system') ? 'true' : 'false'"
|
|
129
|
+
@click="choose('auto')"
|
|
130
|
+
>
|
|
131
|
+
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
132
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
|
|
133
|
+
</svg>
|
|
134
|
+
@if(showLabels)<span>{{ systemLabel }}</span>@endif
|
|
135
|
+
</button>
|
|
136
|
+
</div>
|
|
137
|
+
@else
|
|
138
|
+
<button
|
|
139
|
+
type="button"
|
|
140
|
+
class="{{ buttonClasses }}"
|
|
141
|
+
:aria-label="triggerLabel()"
|
|
142
|
+
:title="triggerLabel()"
|
|
143
|
+
@click="cycle()"
|
|
144
|
+
>
|
|
145
|
+
<svg :show="isActive('light')" class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
146
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
|
|
147
|
+
</svg>
|
|
148
|
+
<svg :show="isActive('dark')" x-cloak class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
149
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
|
|
150
|
+
</svg>
|
|
151
|
+
<svg :show="isActive('system')" x-cloak class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
152
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
|
|
153
|
+
</svg>
|
|
154
|
+
</button>
|
|
155
|
+
@endif
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { default as ThemeToggle } from './ThemeToggle.stx'
|
|
2
|
+
|
|
3
|
+
export interface ThemeToggleProps {
|
|
4
|
+
/** `group` shows light / dark / system; `button` cycles through them. */
|
|
5
|
+
variant?: 'group' | 'button'
|
|
6
|
+
lightLabel?: string
|
|
7
|
+
darkLabel?: string
|
|
8
|
+
systemLabel?: string
|
|
9
|
+
/** Defaults to true for `group`, false for `button`. */
|
|
10
|
+
showLabels?: boolean
|
|
11
|
+
className?: string
|
|
12
|
+
onChange?: (state: { preference: 'light' | 'dark' | 'auto', mode: 'light' | 'dark' }) => void
|
|
13
|
+
}
|
package/src/utils/highlighter.ts
CHANGED
|
@@ -18,6 +18,9 @@ async function loadCreateHighlighter(): Promise<typeof import('ts-syntax-highlig
|
|
|
18
18
|
|
|
19
19
|
let highlighterInstance: TSHighlighter | null = null
|
|
20
20
|
|
|
21
|
+
const LIGHT = 'github-light'
|
|
22
|
+
const DARK = 'github-dark'
|
|
23
|
+
|
|
21
24
|
export interface HighlighterOptions {
|
|
22
25
|
theme?: 'light' | 'dark' | 'auto'
|
|
23
26
|
language?: string
|
|
@@ -61,6 +64,126 @@ export async function getHighlighter(): Promise<TSHighlighter> {
|
|
|
61
64
|
return highlighterInstance
|
|
62
65
|
}
|
|
63
66
|
|
|
67
|
+
/*
|
|
68
|
+
* Matched on the class ATTRIBUTE generally, then filtered, rather than with
|
|
69
|
+
* `class="token …"` written into the pattern: the linter reads that literal as
|
|
70
|
+
* a Tailwind class list and asks for it to be reordered.
|
|
71
|
+
*/
|
|
72
|
+
const STYLED_SPAN = /<span class="([^"]*)"[^>]*style="([^"]*)"/g
|
|
73
|
+
const TOKEN_CLASS = /^token\s+(\S.*)$/
|
|
74
|
+
|
|
75
|
+
/** `color: #rrggbb; font-style: italic` on each token span, in document order. */
|
|
76
|
+
function tokenStyles(html: string): Array<[string, string]> {
|
|
77
|
+
const out: Array<[string, string]> = []
|
|
78
|
+
for (const m of html.matchAll(STYLED_SPAN)) {
|
|
79
|
+
const token = TOKEN_CLASS.exec(m[1] ?? '')
|
|
80
|
+
if (token)
|
|
81
|
+
out.push([token[1]!.trim(), m[2] ?? ''])
|
|
82
|
+
}
|
|
83
|
+
return out
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A CSS class is safe to put in a selector; a scope name from a theme is not. */
|
|
87
|
+
function selectorSafe(cls: string): boolean {
|
|
88
|
+
return /^[\w-]+$/.test(cls)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Both palettes as CSS, with the inline colours stripped so the rules apply.
|
|
93
|
+
*
|
|
94
|
+
* The engine writes token colours inline, which no media query can override, so
|
|
95
|
+
* `auto` could only ever ship one palette (stacksjs/stx#2015). This renders the
|
|
96
|
+
* same code under each theme, pairs the spans by position - they are the same
|
|
97
|
+
* tokens, so the two renders align exactly - and re-emits the colours as rules
|
|
98
|
+
* on the `.token` classes the spans already carry.
|
|
99
|
+
*
|
|
100
|
+
* Returns null when the two renders do not align or nothing is coloured, so a
|
|
101
|
+
* surprise from the engine degrades to the single-palette path rather than to
|
|
102
|
+
* an unstyled block.
|
|
103
|
+
*/
|
|
104
|
+
async function dualTheme(
|
|
105
|
+
highlighter: TSHighlighter,
|
|
106
|
+
code: string,
|
|
107
|
+
language: string,
|
|
108
|
+
): Promise<{ html: string, css: string } | null> {
|
|
109
|
+
let light: { html?: string, css?: string }
|
|
110
|
+
let dark: { html?: string, css?: string }
|
|
111
|
+
try {
|
|
112
|
+
light = await highlighter.highlight(code, language, { theme: LIGHT }) as { html?: string, css?: string }
|
|
113
|
+
dark = await highlighter.highlight(code, language, { theme: DARK }) as { html?: string, css?: string }
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const lightHtml = light?.html
|
|
120
|
+
const darkHtml = dark?.html
|
|
121
|
+
if (!lightHtml || !darkHtml)
|
|
122
|
+
return null
|
|
123
|
+
|
|
124
|
+
const lightStyles = tokenStyles(lightHtml)
|
|
125
|
+
const darkStyles = tokenStyles(darkHtml)
|
|
126
|
+
if (lightStyles.length === 0 || lightStyles.length !== darkStyles.length)
|
|
127
|
+
return null
|
|
128
|
+
|
|
129
|
+
// First occurrence wins: a token class maps to one scope, so every span
|
|
130
|
+
// carrying it resolves to the same colour in a given theme.
|
|
131
|
+
const palette = new Map<string, { light: string, dark: string }>()
|
|
132
|
+
for (let i = 0; i < lightStyles.length; i++) {
|
|
133
|
+
const [cls, lightStyle] = lightStyles[i]!
|
|
134
|
+
const darkStyle = darkStyles[i]![1]
|
|
135
|
+
if (!cls || !lightStyle || !darkStyle || palette.has(cls) || !selectorSafe(cls))
|
|
136
|
+
continue
|
|
137
|
+
palette.set(cls, { light: lightStyle, dark: darkStyle })
|
|
138
|
+
}
|
|
139
|
+
if (palette.size === 0)
|
|
140
|
+
return null
|
|
141
|
+
|
|
142
|
+
const rules: string[] = []
|
|
143
|
+
const darkRules: string[] = []
|
|
144
|
+
for (const [cls, { light: l, dark: d }] of palette) {
|
|
145
|
+
rules.push(`.syntax .token.${cls} { ${l} }`)
|
|
146
|
+
darkRules.push(`.syntax .token.${cls} { ${d} }`)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
* The PANEL as well as the tokens. `.syntax` carries
|
|
151
|
+
* `background-color: #ffffff` from the light stylesheet and the issue names
|
|
152
|
+
* it directly - dark token colours on a white panel is the same bug in
|
|
153
|
+
* reverse, and it is the half a reader notices first. Taken from the engine's
|
|
154
|
+
* own dark stylesheet rather than from the theme's `colors` map, for the same
|
|
155
|
+
* reason the token colours are: its resolution stays authoritative.
|
|
156
|
+
*/
|
|
157
|
+
const darkPanel = /\.syntax\s*\{([^}]*)\}/.exec(dark.css ?? '')?.[1]
|
|
158
|
+
const panelDecls = darkPanel
|
|
159
|
+
? darkPanel.split(';').map(d => d.trim()).filter(d => /^(?:background-color|color)\s*:/.test(d)).join('; ')
|
|
160
|
+
: ''
|
|
161
|
+
if (panelDecls)
|
|
162
|
+
darkRules.unshift(`.syntax { ${panelDecls} }`)
|
|
163
|
+
|
|
164
|
+
/*
|
|
165
|
+
* Both switches, matching how the rest of the library does dark mode: the
|
|
166
|
+
* media query for a viewer following their OS, and `.dark` for an app with a
|
|
167
|
+
* manual toggle. The media query is guarded against a page pinned to light,
|
|
168
|
+
* or an app that chose light on a dark OS would get dark code.
|
|
169
|
+
*/
|
|
170
|
+
const css = [
|
|
171
|
+
light.css ?? '',
|
|
172
|
+
rules.join('\n'),
|
|
173
|
+
`@media (prefers-color-scheme: dark) {\n:root:not(.light) ${darkRules.join('\n:root:not(.light) ')}\n}`,
|
|
174
|
+
darkRules.map(rule => `.dark ${rule}`).join('\n'),
|
|
175
|
+
].filter(Boolean).join('\n')
|
|
176
|
+
|
|
177
|
+
// Stripped, or the inline colour beats every rule above. Same reason as
|
|
178
|
+
// STYLED_SPAN for not writing the class literal into the pattern.
|
|
179
|
+
const html = lightHtml.replace(
|
|
180
|
+
/(<span class="[^"]*")[^>]*?\s+style="[^"]*"/g,
|
|
181
|
+
(whole, open: string) => (TOKEN_CLASS.test(/class="([^"]*)"/.exec(open)?.[1] ?? '') ? open : whole),
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return { html, css }
|
|
185
|
+
}
|
|
186
|
+
|
|
64
187
|
/**
|
|
65
188
|
* Highlight code with syntax highlighting
|
|
66
189
|
*/
|
|
@@ -84,20 +207,30 @@ export async function highlight(
|
|
|
84
207
|
* the plain-text floor was told it got light, which is a second untruth on
|
|
85
208
|
* top of the first (stacksjs/stx#2015).
|
|
86
209
|
*
|
|
87
|
-
* `auto`
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
210
|
+
* `auto` emits BOTH palettes and lets CSS choose, rather than probing
|
|
211
|
+
* matchMedia. `highlight()` runs in `<script server>`, where
|
|
212
|
+
* `globalThis.matchMedia` does not exist and a viewer's preference is not
|
|
213
|
+
* knowable at render time anyway, so selecting a palette in JS cannot answer
|
|
214
|
+
* the question at all - it just always answered "light".
|
|
215
|
+
*
|
|
216
|
+
* `renderDualTheme` is the shape the engine offers for this and does not
|
|
217
|
+
* solve it: its CSS styles the `.syntax` CONTAINER for both modes but leaves
|
|
218
|
+
* every token colour where the highlighter puts it, in an inline `style`
|
|
219
|
+
* attribute that no stylesheet rule can override. That would give a dark
|
|
220
|
+
* panel with light-theme token colours - the unreadable half of the bug,
|
|
221
|
+
* kept. There are no per-token-type rules in its output to override either,
|
|
222
|
+
* in single or dual mode.
|
|
223
|
+
*
|
|
224
|
+
* So `dualTheme()` below renders the same code under each palette, reads
|
|
225
|
+
* back the colours the highlighter itself assigned, and emits them as rules
|
|
226
|
+
* keyed on the token classes already present on the spans - with the inline
|
|
227
|
+
* styles stripped so the rules win. Reading them back rather than resolving
|
|
228
|
+
* the themes' TextMate scopes keeps the engine's own resolution authoritative
|
|
229
|
+
* instead of reimplementing it.
|
|
97
230
|
*/
|
|
98
231
|
const effectiveTheme = theme === 'auto'
|
|
99
232
|
? (globalThis.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'github-dark' : 'github-light')
|
|
100
|
-
: theme === 'dark' ?
|
|
233
|
+
: theme === 'dark' ? DARK : LIGHT
|
|
101
234
|
|
|
102
235
|
// A highlighter that cannot load must not take the code with it. Highlighting
|
|
103
236
|
// is decoration; the code is the content. ts-syntax-highlighter@0.2.17 ships a
|
|
@@ -113,6 +246,17 @@ export async function highlight(
|
|
|
113
246
|
return { html: plainCodeHtml(code), css: '', language, theme: effectiveTheme }
|
|
114
247
|
}
|
|
115
248
|
|
|
249
|
+
/*
|
|
250
|
+
* `auto` means both palettes, chosen by CSS. Falls through to the single
|
|
251
|
+
* palette below if the engine's two renders do not line up, so a surprise
|
|
252
|
+
* degrades to today's behaviour rather than to an unstyled block.
|
|
253
|
+
*/
|
|
254
|
+
if (theme === 'auto') {
|
|
255
|
+
const dual = await dualTheme(highlighter, code, language)
|
|
256
|
+
if (dual)
|
|
257
|
+
return { html: dual.html, css: dual.css, language, theme: 'auto' }
|
|
258
|
+
}
|
|
259
|
+
|
|
116
260
|
// ts-syntax-highlighter returns { html, css, tokens, ansi }. This read the
|
|
117
261
|
// whole result as if it were the HTML string, and the `String(...)` meant to
|
|
118
262
|
// be defensive turned the object into the literal text [object Object] --
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
<script server>
|
|
2
|
-
export const value = $props.value || null
|
|
3
|
-
export const minDate = $props.minDate || null
|
|
4
|
-
export const maxDate = $props.maxDate || null
|
|
5
|
-
export const disabledDates = $props.disabledDates || []
|
|
6
|
-
export const locale = $props.locale || 'en-US'
|
|
7
|
-
export const firstDayOfWeek = $props.firstDayOfWeek || 0
|
|
8
|
-
export const showToday = $props.showToday !== false
|
|
9
|
-
export const className = $props.className || ''
|
|
10
|
-
export const highlightToday = $props.highlightToday !== false
|
|
11
|
-
|
|
12
|
-
const baseClasses = 'bg-panel rounded-panel shadow-lg p-4 w-full max-w-sm'
|
|
13
|
-
export const calendarClasses = `${baseClasses} ${className}`.trim()
|
|
14
|
-
export const headerClasses = 'flex items-center justify-between mb-4'
|
|
15
|
-
export const monthYearClasses = 'text-lg font-semibold text-fg'
|
|
16
|
-
export const buttonClasses = 'p-2 hover:bg-surface-hover rounded-control transition-colors'
|
|
17
|
-
export const weekdaysClasses = 'grid grid-cols-7 gap-1 mb-2'
|
|
18
|
-
export const weekdayClasses = 'text-center text-xs font-medium text-fg-muted py-2'
|
|
19
|
-
export const daysGridClasses = 'grid grid-cols-7 gap-1'
|
|
20
|
-
|
|
21
|
-
const valueIso = value ? new Date(value).toISOString() : null
|
|
22
|
-
export const initialValueIso = valueIso
|
|
23
|
-
const minIso = minDate ? new Date(minDate).toISOString() : null
|
|
24
|
-
const maxIso = maxDate ? new Date(maxDate).toISOString() : null
|
|
25
|
-
export const minIsoValue = minIso
|
|
26
|
-
export const maxIsoValue = maxIso
|
|
27
|
-
export const disabledIsoList = disabledDates.map(d => new Date(d).toISOString())
|
|
28
|
-
</script>
|
|
29
|
-
|
|
30
|
-
<script client>
|
|
31
|
-
const emit = defineEmits()
|
|
32
|
-
const locale = {{ locale }}
|
|
33
|
-
const firstDayOfWeek = {{ firstDayOfWeek }}
|
|
34
|
-
const highlightToday = {{ highlightToday }}
|
|
35
|
-
const minDate = {{ minIsoValue === null ? 'null' : minIsoValue }} ? new Date({{ minIsoValue === null ? 'null' : minIsoValue }}) : null
|
|
36
|
-
const maxDate = {{ maxIsoValue === null ? 'null' : maxIsoValue }} ? new Date({{ maxIsoValue === null ? 'null' : maxIsoValue }}) : null
|
|
37
|
-
const disabledDates = ({{ disabledIsoList }}).map(s => new Date(s))
|
|
38
|
-
const initialValue = {{ initialValueIso === null ? 'null' : initialValueIso }} ? new Date({{ initialValueIso === null ? 'null' : initialValueIso }}) : null
|
|
39
|
-
|
|
40
|
-
const today = new Date()
|
|
41
|
-
const currentMonth = state(initialValue ? new Date(initialValue) : new Date())
|
|
42
|
-
const selectedDate = state(initialValue)
|
|
43
|
-
|
|
44
|
-
function isSameDay(d1, d2) {
|
|
45
|
-
if (!d1 || !d2) return false
|
|
46
|
-
return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function isToday(d) { return isSameDay(d, today) }
|
|
50
|
-
|
|
51
|
-
function isDisabled(d) {
|
|
52
|
-
if (minDate && d < minDate) return true
|
|
53
|
-
if (maxDate && d > maxDate) return true
|
|
54
|
-
return disabledDates.some(x => isSameDay(x, d))
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function getDaysInMonth(d) { return new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate() }
|
|
58
|
-
function getFirstDayOfMonth(d) { return new Date(d.getFullYear(), d.getMonth(), 1).getDay() }
|
|
59
|
-
|
|
60
|
-
const monthYearLabel = derived(() => {
|
|
61
|
-
const m = currentMonth()
|
|
62
|
-
return `${m.toLocaleDateString(locale, { month: 'long' })} ${m.getFullYear()}`
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
const dayNames = (() => {
|
|
66
|
-
const days = []
|
|
67
|
-
const baseDate = new Date(2024, 0, firstDayOfWeek)
|
|
68
|
-
for (let i = 0; i < 7; i++) {
|
|
69
|
-
const day = new Date(baseDate)
|
|
70
|
-
day.setDate(baseDate.getDate() + i)
|
|
71
|
-
days.push(day.toLocaleDateString(locale, { weekday: 'short' }))
|
|
72
|
-
}
|
|
73
|
-
return days
|
|
74
|
-
})()
|
|
75
|
-
|
|
76
|
-
const calendarDays = derived(() => {
|
|
77
|
-
const month = currentMonth()
|
|
78
|
-
const daysInMonth = getDaysInMonth(month)
|
|
79
|
-
const firstDay = (getFirstDayOfMonth(month) - firstDayOfWeek + 7) % 7
|
|
80
|
-
const days = []
|
|
81
|
-
const prev = new Date(month.getFullYear(), month.getMonth() - 1, 1)
|
|
82
|
-
const daysInPrev = getDaysInMonth(prev)
|
|
83
|
-
for (let i = firstDay - 1; i >= 0; i--) {
|
|
84
|
-
days.push({ date: new Date(prev.getFullYear(), prev.getMonth(), daysInPrev - i), isCurrentMonth: false })
|
|
85
|
-
}
|
|
86
|
-
for (let i = 1; i <= daysInMonth; i++) {
|
|
87
|
-
days.push({ date: new Date(month.getFullYear(), month.getMonth(), i), isCurrentMonth: true })
|
|
88
|
-
}
|
|
89
|
-
const remaining = 42 - days.length
|
|
90
|
-
const next = new Date(month.getFullYear(), month.getMonth() + 1, 1)
|
|
91
|
-
for (let i = 1; i <= remaining; i++) {
|
|
92
|
-
days.push({ date: new Date(next.getFullYear(), next.getMonth(), i), isCurrentMonth: false })
|
|
93
|
-
}
|
|
94
|
-
return days
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
function previousMonth() {
|
|
98
|
-
const m = currentMonth()
|
|
99
|
-
currentMonth.set(new Date(m.getFullYear(), m.getMonth() - 1, 1))
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function nextMonthFn() {
|
|
103
|
-
const m = currentMonth()
|
|
104
|
-
currentMonth.set(new Date(m.getFullYear(), m.getMonth() + 1, 1))
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function selectDate(date) {
|
|
108
|
-
if (isDisabled(date)) return
|
|
109
|
-
selectedDate.set(date)
|
|
110
|
-
emit('change', date)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function goToToday() {
|
|
114
|
-
currentMonth.set(new Date(today))
|
|
115
|
-
selectDate(today)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function dayClasses(day) {
|
|
119
|
-
const base = 'flex items-center justify-center h-10 w-10 rounded-control text-sm transition-colors'
|
|
120
|
-
const isSelected = isSameDay(day.date, selectedDate())
|
|
121
|
-
const isTodayDate = isToday(day.date)
|
|
122
|
-
const disabled = isDisabled(day.date)
|
|
123
|
-
let cls = base
|
|
124
|
-
cls += day.isCurrentMonth ? 'text-fg' : 'text-fg-subtle'
|
|
125
|
-
cls += disabled ? ' opacity-50 cursor-not-allowed' : 'cursor-pointer hover:bg-surface-hover'
|
|
126
|
-
if (isSelected) cls += 'bg-accent-solid text-accent-ink hover:bg-accent-solid-hover'
|
|
127
|
-
if (isTodayDate && highlightToday && !isSelected) cls += 'border-2 border-accent'
|
|
128
|
-
return cls
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
defineExpose({ selectedDate, currentMonth, selectDate })
|
|
132
|
-
</script>
|
|
133
|
-
|
|
134
|
-
<div class="{{ calendarClasses }}">
|
|
135
|
-
<div class="{{ headerClasses }}">
|
|
136
|
-
<button
|
|
137
|
-
type="button"
|
|
138
|
-
class="{{ buttonClasses }}"
|
|
139
|
-
@click="previousMonth()"
|
|
140
|
-
aria-label="Previous month"
|
|
141
|
-
>
|
|
142
|
-
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
143
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
|
|
144
|
-
</svg>
|
|
145
|
-
</button>
|
|
146
|
-
|
|
147
|
-
<div class="{{ monthYearClasses }}" x-text="monthYearLabel()"></div>
|
|
148
|
-
|
|
149
|
-
<button
|
|
150
|
-
type="button"
|
|
151
|
-
class="{{ buttonClasses }}"
|
|
152
|
-
@click="nextMonthFn()"
|
|
153
|
-
aria-label="Next month"
|
|
154
|
-
>
|
|
155
|
-
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
156
|
-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
|
157
|
-
</svg>
|
|
158
|
-
</button>
|
|
159
|
-
</div>
|
|
160
|
-
|
|
161
|
-
<div class="{{ weekdaysClasses }}">
|
|
162
|
-
<div :for="d in dayNames" :key="d" class="{{ weekdayClasses }}" x-text="d"></div>
|
|
163
|
-
</div>
|
|
164
|
-
|
|
165
|
-
<div class="{{ daysGridClasses }}">
|
|
166
|
-
<button
|
|
167
|
-
:for="day in calendarDays()"
|
|
168
|
-
:key="day.date.toISOString()"
|
|
169
|
-
type="button"
|
|
170
|
-
:class="dayClasses(day)"
|
|
171
|
-
@click="selectDate(day.date)"
|
|
172
|
-
:aria-label="day.date.toLocaleDateString(locale)"
|
|
173
|
-
:disabled="isDisabled(day.date)"
|
|
174
|
-
x-text="day.date.getDate()"
|
|
175
|
-
></button>
|
|
176
|
-
</div>
|
|
177
|
-
|
|
178
|
-
@if(showToday)
|
|
179
|
-
<div class="mt-4 flex justify-center">
|
|
180
|
-
<button
|
|
181
|
-
type="button"
|
|
182
|
-
class="px-4 py-2 text-sm font-medium text-accent hover:bg-surface-hover rounded-control transition-colors"
|
|
183
|
-
@click="goToToday()"
|
|
184
|
-
>
|
|
185
|
-
Today
|
|
186
|
-
</button>
|
|
187
|
-
</div>
|
|
188
|
-
@endif
|
|
189
|
-
</div>
|