@clarusjs/theme-docs 0.5.1 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/theme-docs",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "A docs theme for clarusjs: layout with header, sidebar, content and on-this-page, design tokens, dark mode, copy buttons and callouts.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -16,7 +16,8 @@
16
16
  "access": "public"
17
17
  },
18
18
  "peerDependencies": {
19
- "clarusjs": "^0.17.0"
19
+ "clarusjs": "^0.19.0",
20
+ "mermaid": ">=11"
20
21
  },
21
22
  "engines": {
22
23
  "node": "^22.12.0 || ^24.0.0 || >=26.0.0"
@@ -40,12 +41,18 @@
40
41
  "devDependencies": {
41
42
  "@tailwindcss/cli": "4.3.5",
42
43
  "@types/node": "^26.6.6",
43
- "clarusjs": "^0.17.0",
44
+ "clarusjs": "^0.19.0",
45
+ "mermaid": "12.3.0",
44
46
  "tailwindcss": "4.3.5",
45
47
  "typescript": "^7.0.2",
46
48
  "vite": "^8.3.8",
47
49
  "vitest": "^5.1.1"
48
50
  },
51
+ "peerDependenciesMeta": {
52
+ "mermaid": {
53
+ "optional": true
54
+ }
55
+ },
49
56
  "scripts": {
50
57
  "types": "node scripts/types.mjs",
51
58
  "test": "vitest run",
@@ -0,0 +1,69 @@
1
+ {{--
2
+ Synced code tabs: markdown({ codeGroups: { tag: 'codeTabs', from: '@clarusjs/theme-docs/code-tabs.tscl' } }) puts it
3
+ in each named group (::: code-group sdk). Picking a tab picks the tab with the same label in every group of that name
4
+ on the page, and remembers it (localStorage, per name) for the next page and the next visit. A group without that
5
+ label keeps its tab. The tab picked stays where it was on screen, though groups above it change height. Without
6
+ JavaScript each group works on its own.
7
+ --}}
8
+
9
+ <script>
10
+ const KEY = 'clarusjs-tabs'
11
+ function saved() {
12
+ try {
13
+ return JSON.parse(localStorage.getItem(KEY) ?? '{}') ?? {}
14
+ } catch {
15
+ return {}
16
+ }
17
+ }
18
+ function wire(el) {
19
+ const group = el.closest('.code-group[data-sync]')
20
+ if (!(group instanceof HTMLElement)) return
21
+ const name = group.dataset.sync ?? ''
22
+ const tabs = () => [...group.querySelectorAll(':scope > input[type="radio"]')].filter((tab) => tab instanceof HTMLInputElement)
23
+ const labelOf = (input) => input.nextElementSibling?.textContent?.trim() ?? ''
24
+ function pick(label) {
25
+ const input = tabs().find((tab) => labelOf(tab) === label)
26
+ if (input && !input.checked) input.checked = true // no change event: the others aren't told again
27
+ }
28
+ // A tab picked before the islands attached (the tabs work without JavaScript): any group of this name showing a tab
29
+ // other than its first. It wins over the remembered one, and is remembered. ponytail: a pick of a first tab looks
30
+ // like the default and isn't seen.
31
+ const early = [...document.querySelectorAll('.code-group[data-sync]')]
32
+ .filter((other) => other instanceof HTMLElement && other.dataset.sync === name)
33
+ .map((other) => other.querySelector(':scope > input[type="radio"]:checked:not(:first-child)'))
34
+ .find((input) => input instanceof HTMLInputElement)
35
+ const remembered = saved()[name]
36
+ if (early) {
37
+ const label = labelOf(early)
38
+ pick(label)
39
+ try {
40
+ localStorage.setItem(KEY, JSON.stringify({ ...saved(), [name]: label }))
41
+ } catch {}
42
+ } else if (typeof remembered === 'string') pick(remembered)
43
+ function changed(event) {
44
+ const input = event.target
45
+ if (!(input instanceof HTMLInputElement) || !input.checked || input.parentElement !== group) return
46
+ const label = labelOf(input)
47
+ const tab = input.nextElementSibling
48
+ const before = tab?.getBoundingClientRect().top ?? 0
49
+ try {
50
+ localStorage.setItem(KEY, JSON.stringify({ ...saved(), [name]: label }))
51
+ } catch {}
52
+ window.dispatchEvent(new CustomEvent('code-tabs:pick', { detail: { name, label, from: group } }))
53
+ const moved = (tab?.getBoundingClientRect().top ?? 0) - before
54
+ if (moved) window.scrollBy(0, moved) // groups above changed height: keep this one under the pointer
55
+ }
56
+ function picked(event) {
57
+ const { name: other, label, from } = event.detail ?? {}
58
+ if (other === name && from !== group) pick(label)
59
+ }
60
+ group.addEventListener('change', changed)
61
+ window.addEventListener('code-tabs:pick', picked)
62
+ return () => {
63
+ group.removeEventListener('change', changed)
64
+ window.removeEventListener('code-tabs:pick', picked)
65
+ }
66
+ }
67
+ </script>
68
+
69
+ <span hidden [use]="wire"></span>
@@ -7,6 +7,7 @@
7
7
  @input progress: string
8
8
 
9
9
  <script>
10
+ import { withoutBase } from 'clarusjs/navigation'
10
11
  import { load, stats } from './progress.ts'
11
12
  let xp = state(0)
12
13
  let ready = state(false)
@@ -16,7 +17,8 @@
16
17
  xp = s.xp
17
18
  ready = true
18
19
  for (const link of document.querySelectorAll('.sidebar ul a[href]')) {
19
- const href = link.getAttribute('href') ?? ''
20
+ // the link has the site's base; lessons are routes, without it
21
+ const href = withoutBase(link.getAttribute('href') ?? '')
20
22
  if (!(link instanceof HTMLElement) || !lessons.includes(href)) continue
21
23
  const status = s.status(href)
22
24
  link.dataset.status = status
@@ -7,6 +7,7 @@
7
7
  @input prev: string = ''
8
8
  @input next: string = ''
9
9
  @input leave: string = '/'
10
+ @input layout: 'split' | 'stacked' = 'split'
10
11
 
11
12
  <script>
12
13
  import { answer, finish, load, save, step } from './progress.ts'
@@ -85,7 +86,7 @@
85
86
  location.href = next || leave
86
87
  }
87
88
  }
88
- // Each step's code and results (code blocks, .result) go in a side column, its text in the main one.
89
+ // Each step's code and results (code blocks, .result) go in a side column, its text in the main one ('split').
89
90
  function split(step) {
90
91
  const side = [...step.children].filter((el) => el.matches('.code-block, .sample, pre, .result'))
91
92
  if (!side.length || step.classList.contains('split')) return
@@ -98,7 +99,7 @@
98
99
  step.classList.add('split')
99
100
  }
100
101
  if (typeof document !== 'undefined' && steps().length) {
101
- steps().forEach(split)
102
+ if (layout === 'split') steps().forEach(split)
102
103
  document.body.classList.add('one-step')
103
104
  const lessonBar = document.querySelector('.lesson-bar')
104
105
  if (lessonBar instanceof HTMLElement) lessonBar.hidden = false
package/src/lesson.tscl CHANGED
@@ -83,6 +83,7 @@
83
83
  [prev]="lessonOf(config, path).prev"
84
84
  [next]="lessonOf(config, path).next"
85
85
  [leave]="lessonOf(config, path).leave"
86
+ [layout]="config.learn?.layout ?? 'split'"
86
87
  />
87
88
  </div>
88
89
  </body>
@@ -0,0 +1,60 @@
1
+ {{--
2
+ Draws the ```mermaid diagram it sits next to: markdown({ mermaid: { tag: 'diagram', from: '@clarusjs/theme-docs/mermaid.tscl' } })
3
+ puts it after each block's <pre class="mermaid">, inside .mermaid-diagram. Loads mermaid (a peer dependency: install
4
+ it beside the theme) when the diagram scrolls into view, draws it in the page's theme, and draws it again when the
5
+ theme changes. Each diagram is its own island, so a page swapped in by navigation draws its own. Until it's drawn, or
6
+ if drawing fails, the source stays readable. The box's data-state is drawn or failed.
7
+ --}}
8
+
9
+ <script>
10
+ function wire(el) {
11
+ const box = el.closest('.mermaid-diagram')
12
+ const pre = box?.querySelector('pre.mermaid')
13
+ if (!box || !pre) return
14
+ const source = pre.textContent ?? ''
15
+ const root = document.documentElement
16
+ const media = matchMedia('(prefers-color-scheme: dark)')
17
+ const dark = () => root.dataset.theme === 'dark' || (root.dataset.theme !== 'light' && media.matches)
18
+ let latest = 0
19
+ let gone = false
20
+ async function draw() {
21
+ const run = ++latest
22
+ try {
23
+ const { default: mermaid } = await import('mermaid')
24
+ mermaid.initialize({ startOnLoad: false, securityLevel: 'strict', theme: dark() ? 'dark' : 'default' })
25
+ const { svg } = await mermaid.render('cl-mermaid-' + Math.random().toString(36).slice(2), source)
26
+ if (gone || run !== latest) return
27
+ let out = box.querySelector('.mermaid-svg')
28
+ if (!out) {
29
+ out = document.createElement('div')
30
+ out.className = 'mermaid-svg'
31
+ out.tabIndex = 0 // it scrolls sideways on a narrow screen: reachable by keyboard
32
+ box.insertBefore(out, pre)
33
+ }
34
+ // securityLevel 'strict': mermaid sanitizes its SVG (DOMPurify), and labels' HTML is escaped
35
+ out.innerHTML = svg
36
+ // Full size (mermaid's max-width), not shrunk to the column: on a phone the text stays readable and the box scrolls.
37
+ const drawing = out.querySelector('svg')
38
+ const natural = parseFloat(drawing?.style.maxWidth ?? '')
39
+ if (drawing && natural) drawing.style.minWidth = natural + 'px'
40
+ pre.hidden = true
41
+ box.dataset.state = 'drawn'
42
+ } catch (error) {
43
+ if (gone || run !== latest) return
44
+ box.dataset.state = 'failed'
45
+ console.warn('[theme-docs] a mermaid diagram could not be drawn:', error)
46
+ }
47
+ }
48
+ const watch = new MutationObserver(draw)
49
+ watch.observe(root, { attributes: true, attributeFilter: ['data-theme'] })
50
+ media.addEventListener('change', draw)
51
+ draw()
52
+ return () => {
53
+ gone = true
54
+ watch.disconnect()
55
+ media.removeEventListener('change', draw)
56
+ }
57
+ }
58
+ </script>
59
+
60
+ <span class="mermaid-draw" [use]="wire"></span>
package/src/nav.ts CHANGED
@@ -62,9 +62,10 @@ export type ThemeConfig = {
62
62
  pages?: readonly PageInfo[]
63
63
  /**
64
64
  * Learn: the section whose pages are lessons, and its progress page (not a lesson). Progress, XP and badges are
65
- * kept in the browser (localStorage); an XP chip shows in lesson headers and on the section's pages.
65
+ * kept in the browser (localStorage); an XP chip shows in lesson headers and on the section's pages. `layout`: from
66
+ * 1024px a step's code goes in a side column beside its text (`'split'`, the default); `'stacked'` keeps it inline.
66
67
  */
67
- learn?: { section: string; progress: string }
68
+ learn?: { section: string; progress: string; layout?: 'split' | 'stacked' }
68
69
  /** A search button (and Ctrl/⌘K, /) in the header; the index comes from the themeDocs() Vite plugin. */
69
70
  search?: boolean
70
71
  /**
@@ -334,6 +334,10 @@ details.custom-block[open] > summary { margin-bottom: 8px; }
334
334
  .code-group > label { order: 0; font: 600 13px var(--mono); padding: 6px 12px; border: 1px solid var(--rule); border-bottom: 0; border-radius: 8px 8px 0 0; color: var(--muted); cursor: pointer; }
335
335
  .code-group > input:checked + label { color: var(--accent); background: var(--code-bg); }
336
336
  .code-group > input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }
337
+ /* ```mermaid diagrams (mermaid.tscl): the source until drawn, then the SVG at full size, scrolling sideways when wider than the column */
338
+ .mermaid-diagram { margin: 0 0 20px; }
339
+ .mermaid-svg { display: flex; overflow-x: auto; }
340
+ .mermaid-svg svg { margin: 0 auto; height: auto; } /* centred; wider than the column, it scrolls (flex centring would cut off its start) */
337
341
  .code-group > .code-block { order: 1; width: 100%; display: none; margin: 0; }
338
342
  .code-group > input:checked + label + .code-block { display: block; }
339
343
  .code-group > .code-block pre { border-top-left-radius: 0; }
package/src/theme.css CHANGED
@@ -1275,6 +1275,17 @@ details.custom-block[open] > summary {
1275
1275
  outline: 2px solid var(--accent);
1276
1276
  outline-offset: 2px;
1277
1277
  }
1278
+ .mermaid-diagram {
1279
+ margin: 0 0 20px;
1280
+ }
1281
+ .mermaid-svg {
1282
+ display: flex;
1283
+ overflow-x: auto;
1284
+ }
1285
+ .mermaid-svg svg {
1286
+ margin: 0 auto;
1287
+ height: auto;
1288
+ }
1278
1289
  .code-group > .code-block {
1279
1290
  order: 1;
1280
1291
  width: 100%;