@clarusjs/theme-docs 0.5.2 → 0.7.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 +13 -6
- package/src/callout.tscl +8 -4
- package/src/code-tabs.tscl +69 -0
- package/src/layout.tscl +31 -26
- package/src/learn-path.tscl +26 -23
- package/src/learn-progress.tscl +8 -4
- package/src/learn-status.tscl +13 -8
- package/src/lesson-bar.tscl +36 -22
- package/src/lesson.css +5 -2
- package/src/lesson.tscl +36 -28
- package/src/mermaid.tscl +60 -0
- package/src/nav.ts +20 -2
- package/src/page-head.tscl +15 -10
- package/src/parts/contents.tscl +11 -7
- package/src/parts/footer.tscl +7 -3
- package/src/parts/header.tscl +19 -15
- package/src/parts/icon.tscl +9 -5
- package/src/parts/page-footer.tscl +14 -10
- package/src/parts/sidebar.tscl +15 -11
- package/src/progress-board.tscl +11 -8
- package/src/quiz-choices.tscl +18 -14
- package/src/quiz.tscl +12 -8
- package/src/search.tscl +2 -1
- package/src/styles/theme.css +16 -0
- package/src/theme.css +41 -0
- package/src/vite.js +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/theme-docs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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.
|
|
19
|
+
"clarusjs": "^0.20.0",
|
|
20
|
+
"mermaid": ">=11"
|
|
20
21
|
},
|
|
21
22
|
"engines": {
|
|
22
23
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
@@ -39,12 +40,18 @@
|
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@tailwindcss/cli": "4.3.5",
|
|
42
|
-
"@types/node": "^26.6.
|
|
43
|
-
"clarusjs": "^0.
|
|
43
|
+
"@types/node": "^26.6.11",
|
|
44
|
+
"clarusjs": "^0.20.0",
|
|
45
|
+
"mermaid": "12.4.0",
|
|
44
46
|
"tailwindcss": "4.3.5",
|
|
45
47
|
"typescript": "^7.0.2",
|
|
46
|
-
"vite": "^8.
|
|
47
|
-
"vitest": "^5.1.
|
|
48
|
+
"vite": "^8.4.0",
|
|
49
|
+
"vitest": "^5.1.2"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"mermaid": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
48
55
|
},
|
|
49
56
|
"scripts": {
|
|
50
57
|
"types": "node scripts/types.mjs",
|
package/src/callout.tscl
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
type Kind = 'tip' | 'info' | 'warning' | 'danger'
|
|
3
|
+
const props = props<{
|
|
4
|
+
type?: Kind
|
|
5
|
+
}>(() => ({ type: 'info' }))
|
|
6
|
+
</script>
|
|
3
7
|
|
|
4
|
-
<aside role="note" class="callout {{ type }}">
|
|
8
|
+
<aside role="note" class="callout {{ props.type }}">
|
|
5
9
|
<p class="callout-title">
|
|
6
|
-
<slot name="title">{{ type[0].toUpperCase() + type.slice(1) }}</slot>
|
|
10
|
+
<slot name="title">{{ props.type[0].toUpperCase() + props.type.slice(1) }}</slot>
|
|
7
11
|
</p>
|
|
8
12
|
<slot />
|
|
9
13
|
</aside>
|
|
@@ -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>
|
package/src/layout.tscl
CHANGED
|
@@ -10,52 +10,57 @@
|
|
|
10
10
|
@import pageContents from './parts/contents.tscl'
|
|
11
11
|
@import pageFooter from './parts/page-footer.tscl'
|
|
12
12
|
@import siteFooter from './parts/footer.tscl'
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
<script lang="ts">
|
|
14
|
+
import { sectionKey, crumb } from './nav.ts'
|
|
15
|
+
import type { ThemeConfig, Heading } from './nav.ts'
|
|
16
|
+
const props = props<{
|
|
17
|
+
config: ThemeConfig
|
|
18
|
+
title: string
|
|
19
|
+
description?: string
|
|
20
|
+
path?: string
|
|
21
|
+
/** The page's own route, when it differs from the sidebar entry it belongs to (e.g. /releases/[version]). */
|
|
22
|
+
source?: string
|
|
23
|
+
/** The page's own address, when path names its sidebar entry instead (e.g. /releases/0.7.0 under /releases). */
|
|
24
|
+
url?: string
|
|
25
|
+
/** The page's h2/h3 for "On this page" (Markdown pages pass them; none: the region is left out). */
|
|
26
|
+
headings?: Heading[]
|
|
27
|
+
/** A tool (a playground) rather than a page: the header and a full-height main, without sidebar, crumb or footers. */
|
|
28
|
+
app?: boolean
|
|
29
|
+
}>(() => ({ description: '', path: '/', source: '', url: '', headings: [], app: false }))
|
|
30
|
+
</script>
|
|
31
|
+
|
|
15
32
|
@import './theme.css'
|
|
16
|
-
@input config: ThemeConfig
|
|
17
|
-
@input title: string
|
|
18
|
-
@input description: string = ''
|
|
19
|
-
@input path: string = '/'
|
|
20
|
-
{{-- The page's own route, when it differs from the sidebar entry it belongs to (e.g. /releases/[version]). --}}
|
|
21
|
-
@input source: string = ''
|
|
22
|
-
{{-- The page's own address, when path names its sidebar entry instead (e.g. /releases/0.7.0 under /releases). --}}
|
|
23
|
-
@input url: string = ''
|
|
24
|
-
{{-- The page's h2/h3 for "On this page" (Markdown pages pass them; none: the region is left out). --}}
|
|
25
|
-
@input headings: Heading[] = []
|
|
26
|
-
{{-- A tool (a playground) rather than a page: the header and a full-height main, without sidebar, crumb or footers. --}}
|
|
27
|
-
@input app: boolean = false
|
|
28
33
|
|
|
29
34
|
<html lang="en">
|
|
30
35
|
<head>
|
|
31
|
-
<pageHead [config]="config" [title]="title" [description]="description" [url]="url || path" />
|
|
36
|
+
<pageHead [config]="props.config" [title]="props.title" [description]="props.description" [url]="props.url || props.path" />
|
|
32
37
|
</head>
|
|
33
|
-
<body>
|
|
38
|
+
<body [data-code-groups]="props.config.codeGroups">
|
|
34
39
|
{{-- First in the tab order: keyboard and screen reader users jump past the header and page list. --}}
|
|
35
40
|
<a class="skip-link" href="#content">Skip to content</a>
|
|
36
|
-
<siteHeader [config]="config" [path]="path" [app]="app">@slot('end') {<slot name="header-end"></slot>}</siteHeader>
|
|
37
|
-
@if (app) {
|
|
41
|
+
<siteHeader [config]="props.config" [path]="props.path" [app]="props.app">@slot('end') {<slot name="header-end"></slot>}</siteHeader>
|
|
42
|
+
@if (props.app) {
|
|
38
43
|
<main class="content app" id="content" tabindex="-1"><slot /></main>
|
|
39
|
-
} @else if (sectionKey(config.sections, path)) {
|
|
44
|
+
} @else if (sectionKey(props.config.sections, props.path)) {
|
|
40
45
|
<div class="docs">
|
|
41
46
|
{{-- A drawer below 1024px (popover: Escape and a click outside close it), always visible from 1024px. --}}
|
|
42
|
-
<docsSidebar [config]="config" [path]="path">@slot('top') {<slot name="sidebar-top"></slot>}@slot('bottom') {<slot name="sidebar-bottom"></slot>}</docsSidebar>
|
|
47
|
+
<docsSidebar [config]="props.config" [path]="props.path">@slot('top') {<slot name="sidebar-top"></slot>}@slot('bottom') {<slot name="sidebar-bottom"></slot>}</docsSidebar>
|
|
43
48
|
{{-- tabindex -1: the skip link (and navigation) can move focus here. --}}
|
|
44
49
|
<main class="content" id="content" tabindex="-1">
|
|
45
|
-
<p class="crumb">{{ crumb(config.sections, path) }}</p>
|
|
46
|
-
<pageContents [headings]="headings" [inline]="true" />
|
|
50
|
+
<p class="crumb">{{ crumb(props.config.sections, props.path) }}</p>
|
|
51
|
+
<pageContents [headings]="props.headings" [inline]="true" />
|
|
47
52
|
<slot name="content-top"></slot>
|
|
48
53
|
<slot />
|
|
49
54
|
<slot name="content-bottom"></slot>
|
|
50
|
-
<pageFooter [config]="config" [path]="path" [source]="source" />
|
|
55
|
+
<pageFooter [config]="props.config" [path]="props.path" [source]="props.source" />
|
|
51
56
|
</main>
|
|
52
|
-
<pageContents [headings]="headings" />
|
|
57
|
+
<pageContents [headings]="props.headings" />
|
|
53
58
|
</div>
|
|
54
59
|
} @else {
|
|
55
60
|
<main class="content home" id="content" tabindex="-1"><slot /></main>
|
|
56
61
|
}
|
|
57
|
-
@if (!app) {
|
|
58
|
-
<siteFooter [config]="config"><slot name="footer">@if (config.footer) {<p>{{ config.footer }}</p>}</slot></siteFooter>
|
|
62
|
+
@if (!props.app) {
|
|
63
|
+
<siteFooter [config]="props.config"><slot name="footer">@if (props.config.footer) {<p>{{ props.config.footer }}</p>}</slot></siteFooter>
|
|
59
64
|
}
|
|
60
65
|
</body>
|
|
61
66
|
</html>
|
package/src/learn-path.tscl
CHANGED
|
@@ -4,17 +4,20 @@
|
|
|
4
4
|
XP and badges. Progress is read from this browser, so without JavaScript only the lessons and steps show.
|
|
5
5
|
The steps follow the lesson bar (learn:step); everything else follows saved progress (learn:progress).
|
|
6
6
|
--}}
|
|
7
|
-
@import type { Heading } from './nav.ts'
|
|
8
|
-
@interface Lesson { title: string; href: string; xp?: number; minutes?: number }
|
|
9
|
-
@input lessons: Lesson[]
|
|
10
|
-
@input path: string
|
|
11
|
-
@input eyebrow: string = 'Learn'
|
|
12
|
-
@input course: string = ''
|
|
13
|
-
@input steps: Heading[] = []
|
|
14
|
-
@input progress: string = ''
|
|
15
7
|
|
|
16
|
-
<script>
|
|
8
|
+
<script lang="ts">
|
|
17
9
|
import { load, stats } from './progress.ts'
|
|
10
|
+
import type { Heading } from './nav.ts'
|
|
11
|
+
import { browser } from 'clarusjs/runtime'
|
|
12
|
+
interface Lesson { title: string; href: string; xp?: number; minutes?: number }
|
|
13
|
+
const props = props<{
|
|
14
|
+
lessons: Lesson[]
|
|
15
|
+
path: string
|
|
16
|
+
eyebrow?: string
|
|
17
|
+
course?: string
|
|
18
|
+
steps?: Heading[]
|
|
19
|
+
progress?: string
|
|
20
|
+
}>(() => ({ eyebrow: 'Learn', course: '', steps: [], progress: '' }))
|
|
18
21
|
let ready = state(false)
|
|
19
22
|
let statuses = state(Object.create(null)) // lesson href → done, current, next or locked
|
|
20
23
|
let done = state(0)
|
|
@@ -24,18 +27,18 @@
|
|
|
24
27
|
function refresh() {
|
|
25
28
|
const s = stats(
|
|
26
29
|
load(),
|
|
27
|
-
lessons.map((l) => l.href),
|
|
30
|
+
props.lessons.map((l) => l.href),
|
|
28
31
|
)
|
|
29
|
-
const current = lessons.findIndex((l) => s.status(l.href) === 'current')
|
|
32
|
+
const current = props.lessons.findIndex((l) => s.status(l.href) === 'current')
|
|
30
33
|
statuses = Object.fromEntries(
|
|
31
|
-
lessons.map((l, i) => [l.href, s.status(l.href) === 'locked' && i === current + 1 ? 'next' : s.status(l.href)]),
|
|
34
|
+
props.lessons.map((l, i) => [l.href, s.status(l.href) === 'locked' && i === current + 1 ? 'next' : s.status(l.href)]),
|
|
32
35
|
)
|
|
33
36
|
done = s.done
|
|
34
37
|
xp = s.xp
|
|
35
38
|
earned = s.badges.filter((b) => b.earned).length
|
|
36
39
|
ready = true
|
|
37
40
|
}
|
|
38
|
-
const note = (lesson, i) =>
|
|
41
|
+
const note = (lesson: Lesson, i: number) =>
|
|
39
42
|
ready && statuses[lesson.href] === 'done'
|
|
40
43
|
? 'Done'
|
|
41
44
|
: ready && statuses[lesson.href] === 'locked'
|
|
@@ -43,7 +46,7 @@
|
|
|
43
46
|
: lesson.minutes
|
|
44
47
|
? lesson.minutes + ' min'
|
|
45
48
|
: ''
|
|
46
|
-
if (
|
|
49
|
+
if (browser) {
|
|
47
50
|
refresh()
|
|
48
51
|
window.addEventListener('learn:progress', refresh)
|
|
49
52
|
window.addEventListener('learn:step', (event) => {
|
|
@@ -54,25 +57,25 @@
|
|
|
54
57
|
|
|
55
58
|
<nav class="learn-path flex flex-col" aria-label="Lessons">
|
|
56
59
|
<div class="border-b border-rule px-4 pt-5 pb-4">
|
|
57
|
-
<p class="m-0 text-[11px] font-bold tracking-wider text-accent uppercase">{{ eyebrow }}</p>
|
|
58
|
-
<p class="m-0 mt-0.5 text-[17px] font-bold text-fg">{{ course }}</p>
|
|
60
|
+
<p class="m-0 text-[11px] font-bold tracking-wider text-accent uppercase">{{ props.eyebrow }}</p>
|
|
61
|
+
<p class="m-0 mt-0.5 text-[17px] font-bold text-fg">{{ props.course }}</p>
|
|
59
62
|
<div class="mt-3 flex items-center gap-2.5">
|
|
60
63
|
<progress
|
|
61
64
|
class="path-progress h-1.5 flex-1"
|
|
62
65
|
[value]="done"
|
|
63
|
-
[max]="lessons.length"
|
|
66
|
+
[max]="props.lessons.length"
|
|
64
67
|
aria-label="Lessons done"
|
|
65
68
|
></progress>
|
|
66
|
-
<span class="text-[13px] font-semibold text-fg">{{ done }} of {{ lessons.length }}</span>
|
|
69
|
+
<span class="text-[13px] font-semibold text-fg">{{ done }} of {{ props.lessons.length }}</span>
|
|
67
70
|
</div>
|
|
68
71
|
</div>
|
|
69
72
|
<ol class="m-0 flex-1 list-none overflow-y-auto p-3">
|
|
70
|
-
@for (lesson of lessons; track lesson.href) {
|
|
73
|
+
@for (lesson of props.lessons; track lesson.href) {
|
|
71
74
|
<li class="m-0 mb-1" [data-status]="ready ? statuses[lesson.href] : false">
|
|
72
75
|
<a
|
|
73
76
|
class="path-lesson flex items-center gap-3 rounded-xl border-2 border-transparent px-2.5 py-2.5 text-fg no-underline aria-[current=page]:border-accent"
|
|
74
77
|
[href]="lesson.href"
|
|
75
|
-
[aria-current]="lesson.href === path ? 'page' : false"
|
|
78
|
+
[aria-current]="lesson.href === props.path ? 'page' : false"
|
|
76
79
|
>
|
|
77
80
|
<span
|
|
78
81
|
class="path-dot grid size-7 flex-none place-items-center rounded-full text-xs font-bold"
|
|
@@ -103,9 +106,9 @@
|
|
|
103
106
|
<span class="path-xp text-xs font-bold">+{{ lesson.xp }}</span>
|
|
104
107
|
}
|
|
105
108
|
</a>
|
|
106
|
-
@if (lesson.href === path && steps.length) {
|
|
109
|
+
@if (lesson.href === props.path && props.steps.length) {
|
|
107
110
|
<ol class="path-steps my-1.5 ml-[46px] list-none p-0" aria-label="Steps">
|
|
108
|
-
@for (s of steps; track s.id) {
|
|
111
|
+
@for (s of props.steps; track s.id) {
|
|
109
112
|
<li class="m-0" [data-step]="$index < step ? 'done' : $index === step ? 'current' : 'next'">
|
|
110
113
|
<a
|
|
111
114
|
class="flex items-center gap-2.5 py-1 text-[13px] text-muted no-underline"
|
|
@@ -122,7 +125,7 @@
|
|
|
122
125
|
</ol>
|
|
123
126
|
<a
|
|
124
127
|
class="path-footer m-3 mt-0 flex items-center gap-2 rounded-xl px-3.5 py-3 text-sm font-bold no-underline"
|
|
125
|
-
[href]="progress"
|
|
128
|
+
[href]="props.progress"
|
|
126
129
|
>
|
|
127
130
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
128
131
|
<path d="M13 2L4 14h7l-1 8 9-12h-7z"></path>
|
package/src/learn-progress.tscl
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{{-- The progress page's board (put it on the page at config.learn.progress): XP, lessons, quiz accuracy, badges, Reset. --}}
|
|
2
2
|
@import progressBoard from './progress-board.tscl'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
import { lessonsOf } from './nav.ts'
|
|
5
|
+
import type { ThemeConfig } from './nav.ts'
|
|
6
|
+
const props = props<{
|
|
7
|
+
config: ThemeConfig
|
|
8
|
+
}>()
|
|
9
|
+
</script>
|
|
6
10
|
|
|
7
|
-
<progressBoard [lessons]="lessonsOf(config)" />
|
|
11
|
+
<progressBoard [lessons]="lessonsOf(props.config)" />
|
package/src/learn-status.tscl
CHANGED
|
@@ -3,21 +3,26 @@
|
|
|
3
3
|
the Learn sidebar each lesson marked done, current or locked (a hint: every lesson stays open). Hidden until it has
|
|
4
4
|
read the progress, so pages without JavaScript show no "0 XP". Updates when the lesson bar saves (learn:progress).
|
|
5
5
|
--}}
|
|
6
|
-
@input lessons: string[]
|
|
7
|
-
@input progress: string
|
|
8
6
|
|
|
9
|
-
<script>
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { withoutBase } from 'clarusjs/navigation'
|
|
10
9
|
import { load, stats } from './progress.ts'
|
|
10
|
+
import { browser } from 'clarusjs/runtime'
|
|
11
|
+
const props = props<{
|
|
12
|
+
lessons: string[]
|
|
13
|
+
progress: string
|
|
14
|
+
}>()
|
|
11
15
|
let xp = state(0)
|
|
12
16
|
let ready = state(false)
|
|
13
17
|
const words = { done: 'done', current: 'up next', locked: 'not started' }
|
|
14
18
|
function refresh() {
|
|
15
|
-
const s = stats(load(), lessons)
|
|
19
|
+
const s = stats(load(), props.lessons)
|
|
16
20
|
xp = s.xp
|
|
17
21
|
ready = true
|
|
18
22
|
for (const link of document.querySelectorAll('.sidebar ul a[href]')) {
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
// the link has the site's base; lessons are routes, without it
|
|
24
|
+
const href = withoutBase(link.getAttribute('href') ?? '')
|
|
25
|
+
if (!(link instanceof HTMLElement) || !props.lessons.includes(href)) continue
|
|
21
26
|
const status = s.status(href)
|
|
22
27
|
link.dataset.status = status
|
|
23
28
|
let note = link.querySelector('.status-note')
|
|
@@ -29,13 +34,13 @@
|
|
|
29
34
|
note.textContent = ' (' + words[status] + ')'
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
|
-
if (
|
|
37
|
+
if (browser) {
|
|
33
38
|
refresh()
|
|
34
39
|
window.addEventListener('learn:progress', refresh)
|
|
35
40
|
}
|
|
36
41
|
</script>
|
|
37
42
|
|
|
38
|
-
<a class="xp-chip" [href]="progress" [hidden]="!ready" [aria-label]="xp + ' XP: your progress'">
|
|
43
|
+
<a class="xp-chip" [href]="props.progress" [hidden]="!ready" [aria-label]="xp + ' XP: your progress'">
|
|
39
44
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
40
45
|
<path d="M13 2L4 14h7l-1 8 9-12h-7z"></path>
|
|
41
46
|
</svg>
|
package/src/lesson-bar.tscl
CHANGED
|
@@ -3,15 +3,21 @@
|
|
|
3
3
|
follows the URL's #step (the header's segments link there), and after the last step goes to the next lesson.
|
|
4
4
|
It reveals itself (and the one-step view) when it attaches: without JavaScript the lesson is a plain page.
|
|
5
5
|
--}}
|
|
6
|
-
@input lesson: string = ''
|
|
7
|
-
@input prev: string = ''
|
|
8
|
-
@input next: string = ''
|
|
9
|
-
@input leave: string = '/'
|
|
10
6
|
|
|
11
|
-
<script>
|
|
12
|
-
import { answer, finish, load, save, step } from './progress.ts'
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { answer, finish, load, save, step, type Progress } from './progress.ts'
|
|
9
|
+
import { browser } from 'clarusjs/runtime'
|
|
10
|
+
import { withBase } from 'clarusjs/navigation'
|
|
11
|
+
import type { LessonLayout } from './nav.ts'
|
|
12
|
+
const props = props<{
|
|
13
|
+
lesson?: string
|
|
14
|
+
prev?: string
|
|
15
|
+
next?: string
|
|
16
|
+
leave?: string
|
|
17
|
+
layout?: LessonLayout
|
|
18
|
+
}>(() => ({ lesson: '', prev: '', next: '', leave: '/', layout: 'split' }))
|
|
13
19
|
// Progress lives in this browser; the XP chip and the progress page listen for learn:progress.
|
|
14
|
-
function record(update) {
|
|
20
|
+
function record(update: (p: Progress) => Progress): void {
|
|
15
21
|
save(update(load()))
|
|
16
22
|
window.dispatchEvent(new Event('learn:progress'))
|
|
17
23
|
}
|
|
@@ -24,7 +30,7 @@
|
|
|
24
30
|
let title = state('')
|
|
25
31
|
let text = state('')
|
|
26
32
|
const bar = () => document.querySelector('.lesson-bar')
|
|
27
|
-
function verdict(kind, heading, detail) {
|
|
33
|
+
function verdict(kind: string, heading: string, detail: string): void {
|
|
28
34
|
result = kind
|
|
29
35
|
title = heading
|
|
30
36
|
text = detail
|
|
@@ -32,7 +38,7 @@
|
|
|
32
38
|
bar()?.classList.toggle('bad', kind === 'bad')
|
|
33
39
|
}
|
|
34
40
|
const steps = () => [...document.querySelectorAll('main .step')].filter((el) => el instanceof HTMLElement)
|
|
35
|
-
function show(i, focus) {
|
|
41
|
+
function show(i: number, focus: boolean): void {
|
|
36
42
|
const all = steps()
|
|
37
43
|
if (!all.length) return
|
|
38
44
|
current = Math.max(0, Math.min(i, all.length - 1))
|
|
@@ -59,7 +65,7 @@
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
// The step a #hash points into: its own heading, or anything inside it.
|
|
62
|
-
const indexOf = (hash) => {
|
|
68
|
+
const indexOf = (hash: string): number => {
|
|
63
69
|
const id = decodeURIComponent(hash.slice(1))
|
|
64
70
|
return id
|
|
65
71
|
? steps().findIndex((s) => s.getAttribute('aria-labelledby') === id || s.querySelector('#' + CSS.escape(id)))
|
|
@@ -67,7 +73,7 @@
|
|
|
67
73
|
}
|
|
68
74
|
function back() {
|
|
69
75
|
if (current > 0) show(current - 1, true)
|
|
70
|
-
else if (prev) location.href = prev
|
|
76
|
+
else if (props.prev) location.href = withBase(props.prev)
|
|
71
77
|
}
|
|
72
78
|
function forward() {
|
|
73
79
|
if (checking) {
|
|
@@ -78,16 +84,17 @@
|
|
|
78
84
|
}
|
|
79
85
|
const id = steps()[current]?.getAttribute('aria-labelledby')
|
|
80
86
|
if (current < total - 1) {
|
|
81
|
-
if (id) record((p) => step(p, lesson, id))
|
|
87
|
+
if (id) record((p) => step(p, props.lesson, id))
|
|
82
88
|
show(current + 1, true)
|
|
83
89
|
} else {
|
|
84
|
-
record((p) => finish(id ? step(p, lesson, id) : p, lesson))
|
|
85
|
-
location.href = next || leave
|
|
90
|
+
record((p) => finish(id ? step(p, props.lesson, id) : p, props.lesson))
|
|
91
|
+
location.href = withBase(props.next || props.leave)
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
|
-
// Each step's code and results (code blocks, .result) go in a side column, its text in the main one
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
// Each step's code and results (code blocks, whole code groups, .result) go in a side column, its text in the main one (every layout
|
|
95
|
+
// but 'stacked'); CSS places the two by the step's classes.
|
|
96
|
+
function split(step: Element): void {
|
|
97
|
+
const side = [...step.children].filter((el) => el.matches('.code-block, .code-group, .sample, pre, .result'))
|
|
91
98
|
if (!side.length || step.classList.contains('split')) return
|
|
92
99
|
const text = document.createElement('div')
|
|
93
100
|
text.className = 'step-text'
|
|
@@ -96,9 +103,16 @@
|
|
|
96
103
|
for (const el of [...step.children]) (side.includes(el) ? aside : text).append(el)
|
|
97
104
|
step.append(text, aside)
|
|
98
105
|
step.classList.add('split')
|
|
106
|
+
if (props.layout === 'split-reverse') step.classList.add('reverse') // code column on the left (CSS only)
|
|
107
|
+
// stacked-reverse: the heading, then the code, then the text, in the document too (reading order = what you see)
|
|
108
|
+
if (props.layout === 'stacked-reverse') {
|
|
109
|
+
step.classList.add('stacked')
|
|
110
|
+
const h2 = text.firstElementChild?.matches('h2') ? text.firstElementChild : null
|
|
111
|
+
step.prepend(...(h2 ? [h2] : []), aside)
|
|
112
|
+
}
|
|
99
113
|
}
|
|
100
|
-
if (
|
|
101
|
-
steps().forEach(split)
|
|
114
|
+
if (browser && steps().length) {
|
|
115
|
+
if (props.layout !== 'stacked') steps().forEach(split)
|
|
102
116
|
document.body.classList.add('one-step')
|
|
103
117
|
const lessonBar = document.querySelector('.lesson-bar')
|
|
104
118
|
if (lessonBar instanceof HTMLElement) lessonBar.hidden = false
|
|
@@ -124,7 +138,7 @@
|
|
|
124
138
|
const { ok, done, text: detail } = event.detail
|
|
125
139
|
if (!done) return verdict('', 'Pick an answer', detail)
|
|
126
140
|
const quiz = event.target.dataset.quiz
|
|
127
|
-
record((p) => answer(p, lesson + '#' + quiz, ok))
|
|
141
|
+
record((p) => answer(p, props.lesson + '#' + quiz, ok))
|
|
128
142
|
checking = !!steps()[current].querySelector('[data-quiz]:not([data-answered])')
|
|
129
143
|
verdict(ok ? 'ok' : 'bad', ok ? 'Correct!' : 'Not quite', detail)
|
|
130
144
|
})
|
|
@@ -133,7 +147,7 @@
|
|
|
133
147
|
</script>
|
|
134
148
|
|
|
135
149
|
<div class="lesson-actions">
|
|
136
|
-
<button type="button" class="back" (click)="back()" [disabled]="current === 0 && !prev">Back</button>
|
|
150
|
+
<button type="button" class="back" (click)="back()" [disabled]="current === 0 && !props.prev">Back</button>
|
|
137
151
|
<div class="feedback" role="status">
|
|
138
152
|
@if (title) {
|
|
139
153
|
<p class="feedback-title">{{ title }}</p>
|
|
@@ -142,5 +156,5 @@
|
|
|
142
156
|
<p class="feedback-title">Step {{ current + 1 }} of {{ total }}: {{ name }}</p>
|
|
143
157
|
}
|
|
144
158
|
</div>
|
|
145
|
-
<button type="button" class="primary" (click)="forward()">@if (checking) {Check} @else if (total && current === total - 1) {@if (next) {Next lesson} @else {Finish}} @else {Continue}</button>
|
|
159
|
+
<button type="button" class="primary" (click)="forward()">@if (checking) {Check} @else if (total && current === total - 1) {@if (props.next) {Next lesson} @else {Finish}} @else {Continue}</button>
|
|
146
160
|
</div>
|
package/src/lesson.css
CHANGED
|
@@ -37,8 +37,11 @@
|
|
|
37
37
|
/* Wide screens: what the step says on the left, its code and results on the right (the bar island groups them into
|
|
38
38
|
.step-text and .step-side; without JavaScript a step is one column). */
|
|
39
39
|
@media (min-width: 1024px) {
|
|
40
|
-
.lesson .step.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 40px; align-items: start; }
|
|
41
|
-
.step-side { position: sticky; top: calc(var(--header-height) + 16px); padding-top: 24px; }
|
|
40
|
+
.lesson .step.split:not(.stacked) { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 40px; align-items: start; }
|
|
41
|
+
.step.split:not(.stacked) > .step-side { position: sticky; top: calc(var(--header-height) + 16px); padding-top: 24px; }
|
|
42
|
+
/* split-reverse: code on the left, text on the right; the document keeps the text first. */
|
|
43
|
+
.lesson .step.split.reverse > .step-side { grid-column: 1; grid-row: 1; }
|
|
44
|
+
.lesson .step.split.reverse > .step-text { grid-column: 2; grid-row: 1; }
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
@media (prefers-reduced-motion: no-preference) {
|
package/src/lesson.tscl
CHANGED
|
@@ -9,24 +9,31 @@
|
|
|
9
9
|
@import learnStatus from './learn-status.tscl'
|
|
10
10
|
@import learnPath from './learn-path.tscl'
|
|
11
11
|
@import pageHead from './page-head.tscl'
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { lessonOf, lessonsOf, lessonLayoutOf } from './nav.ts'
|
|
14
|
+
import type { ThemeConfig, Heading } from './nav.ts'
|
|
15
|
+
const props = props<{
|
|
16
|
+
config: ThemeConfig
|
|
17
|
+
title: string
|
|
18
|
+
description?: string
|
|
19
|
+
path?: string
|
|
20
|
+
headings?: Heading[]
|
|
21
|
+
/** The page's frontmatter (from @clarusjs/content): `lessonLayout` picks this lesson's layout. */
|
|
22
|
+
frontmatter?: Record<string, unknown>
|
|
23
|
+
}>(() => ({ description: '', path: '/', headings: [], frontmatter: {} }))
|
|
24
|
+
</script>
|
|
25
|
+
|
|
14
26
|
@import './theme.css'
|
|
15
27
|
@import './lesson.css'
|
|
16
|
-
@input config: ThemeConfig
|
|
17
|
-
@input title: string
|
|
18
|
-
@input description: string = ''
|
|
19
|
-
@input path: string = '/'
|
|
20
|
-
@input headings: Heading[] = []
|
|
21
28
|
|
|
22
29
|
<html lang="en">
|
|
23
30
|
<head>
|
|
24
|
-
<pageHead [config]="config" [title]="title" [description]="description" [url]="path" />
|
|
31
|
+
<pageHead [config]="props.config" [title]="props.title" [description]="props.description" [url]="props.path" />
|
|
25
32
|
</head>
|
|
26
|
-
<body class="lesson-page">
|
|
33
|
+
<body class="lesson-page" [data-code-groups]="props.config.codeGroups">
|
|
27
34
|
<a class="skip-link" href="#content">Skip to content</a>
|
|
28
35
|
<header class="lesson-header">
|
|
29
|
-
<a class="leave" [href]="lessonOf(config, path).leave" aria-label="Leave the lesson">
|
|
36
|
+
<a class="leave" [href]="lessonOf(props.config, props.path).leave" aria-label="Leave the lesson">
|
|
30
37
|
<svg
|
|
31
38
|
width="20"
|
|
32
39
|
height="20"
|
|
@@ -41,37 +48,37 @@
|
|
|
41
48
|
</svg>
|
|
42
49
|
</a>
|
|
43
50
|
<div class="lesson-name">
|
|
44
|
-
@if (lessonOf(config, path).total) {
|
|
45
|
-
<p class="eyebrow">{{ lessonOf(config, path).label }}</p>
|
|
51
|
+
@if (lessonOf(props.config, props.path).total) {
|
|
52
|
+
<p class="eyebrow">{{ lessonOf(props.config, props.path).label }}</p>
|
|
46
53
|
}
|
|
47
|
-
<p class="name">{{ title }}</p>
|
|
54
|
+
<p class="name">{{ props.title }}</p>
|
|
48
55
|
</div>
|
|
49
56
|
{{-- Links to each step: jumps without JavaScript, the progress bar with it. --}}
|
|
50
57
|
<ol class="segments" aria-label="Steps">
|
|
51
|
-
@for (h of headings.filter((h) => h.level === 2); track h.id) {
|
|
58
|
+
@for (h of props.headings.filter((h) => h.level === 2); track h.id) {
|
|
52
59
|
<li>
|
|
53
60
|
<a [href]="'#' + h.id">{{ h.text }}</a>
|
|
54
61
|
</li>
|
|
55
62
|
}
|
|
56
63
|
</ol>
|
|
57
|
-
@if (config.learn) {<learnStatus [lessons]="lessonsOf(config).map((l) => l.href)" [progress]="config.learn.progress" />}
|
|
64
|
+
@if (props.config.learn) {<learnStatus [lessons]="lessonsOf(props.config).map((l) => l.href)" [progress]="props.config.learn.progress" />}
|
|
58
65
|
<slot name="header-end"></slot>
|
|
59
66
|
</header>
|
|
60
67
|
<div class="lesson-body">
|
|
61
68
|
{{-- The path (#371): a lesson of config.learn's section, from 1024px. --}}
|
|
62
|
-
@if (config.learn && lessonsOf(config).some((l) => l.href === path)) {
|
|
69
|
+
@if (props.config.learn && lessonsOf(props.config).some((l) => l.href === props.path)) {
|
|
63
70
|
<aside class="lesson-path">
|
|
64
71
|
<learnPath
|
|
65
|
-
[lessons]="lessonsOf(config)"
|
|
66
|
-
[path]="path"
|
|
67
|
-
[eyebrow]="config.sections.find((s) => s.key === config.learn?.section)?.title ?? 'Learn'"
|
|
72
|
+
[lessons]="lessonsOf(props.config)"
|
|
73
|
+
[path]="props.path"
|
|
74
|
+
[eyebrow]="props.config.sections.find((s) => s.key === props.config.learn?.section)?.title ?? 'Learn'"
|
|
68
75
|
[course]="
|
|
69
|
-
config.sections
|
|
70
|
-
.find((s) => s.key === config.learn?.section)
|
|
71
|
-
?.groups.find((g) => g.items.some((i) => i.href === path))?.title ?? ''
|
|
76
|
+
props.config.sections
|
|
77
|
+
.find((s) => s.key === props.config.learn?.section)
|
|
78
|
+
?.groups.find((g) => g.items.some((i) => i.href === props.path))?.title ?? ''
|
|
72
79
|
"
|
|
73
|
-
[steps]="headings.filter((h) => h.level === 2)"
|
|
74
|
-
[progress]="config.learn.progress"
|
|
80
|
+
[steps]="props.headings.filter((h) => h.level === 2)"
|
|
81
|
+
[progress]="props.config.learn.progress"
|
|
75
82
|
/>
|
|
76
83
|
</aside>
|
|
77
84
|
}
|
|
@@ -79,10 +86,11 @@
|
|
|
79
86
|
</div>
|
|
80
87
|
<div class="lesson-bar" role="region" aria-label="Lesson actions" hidden>
|
|
81
88
|
<lessonBar
|
|
82
|
-
[lesson]="path"
|
|
83
|
-
[prev]="lessonOf(config, path).prev"
|
|
84
|
-
[next]="lessonOf(config, path).next"
|
|
85
|
-
[leave]="lessonOf(config, path).leave"
|
|
89
|
+
[lesson]="props.path"
|
|
90
|
+
[prev]="lessonOf(props.config, props.path).prev"
|
|
91
|
+
[next]="lessonOf(props.config, props.path).next"
|
|
92
|
+
[leave]="lessonOf(props.config, props.path).leave"
|
|
93
|
+
[layout]="lessonLayoutOf(props.config, props.frontmatter)"
|
|
86
94
|
/>
|
|
87
95
|
</div>
|
|
88
96
|
</body>
|