Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit b671df0

Browse files
committed
docs: make examples components
1 parent 42987f7 commit b671df0

16 files changed

Lines changed: 304 additions & 178 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//----------------------------------------------------------
2+
// Change Favicon
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// For changing the tab icon.
8+
var changeFavicon = (src) => {
9+
var oldLink = document.querySelector('link[rel="icon"]')
10+
document.head.removeChild(oldLink)
11+
var link = document.createElement('link')
12+
link.rel = 'icon'
13+
link.type = 'image/png'
14+
link.href = src
15+
document.head.appendChild(link)
16+
}
17+
18+
// Listen to the color mode and update the UI.
19+
mode.subscribe((m) => changeFavicon(`images/${m}.png`))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//----------------------------------------------------------
2+
// Cycle Button
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// Get some elements we will use.
8+
var cycleEls = document.querySelectorAll('.pdm-cycle')
9+
10+
// These elements will cycle through all modes.
11+
cycleEls.forEach((el) => {
12+
el.addEventListener('click', (e) =>
13+
mode.update(
14+
(mode, modes, modeIndex) => modes[(modeIndex + 1) % modes.length],
15+
),
16+
)
17+
})

packages/docs/components/emoji.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//----------------------------------------------------------
2+
// Emoji
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// Get some elements we will use.
8+
var emojiEls = document.querySelectorAll('.pdm-emoji')
9+
10+
// Listen to the color mode and update the UI.
11+
mode.subscribe((m) =>
12+
emojiEls.forEach((el) => (el.className = `pdm-emoji emoji ${m}`)),
13+
)
14+
15+
// At this point our callback will have been called,
16+
// so the text of these will be correct and we can show them.
17+
emojiEls.forEach((el) => (el.style.visibility = 'unset'))
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
const subscribeButtons = document.querySelectorAll('.subscribe')
1+
//----------------------------------------------------------
2+
// Form
3+
//----------------------------------------------------------
24

5+
// Get the elements.
6+
var subscribeButtons = document.querySelectorAll('.subscribe')
7+
8+
// Handle submit functions.
39
subscribeButtons.forEach((el) => {
410
el.addEventListener('submit', (e) => {
511
e.preventDefault()
6-
const formId = 1747454
7-
const name = e.target.elements.name.value
8-
const email = e.target.elements.email.value
12+
var formId = 1747454
13+
var name = e.target.elements.name.value
14+
var email = e.target.elements.email.value
915
document
1016
.querySelectorAll('.subscribed')
1117
.forEach((el) => el.classList.remove('hidden'))

packages/docs/components/label.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//----------------------------------------------------------
2+
// Label
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// Get some elements we will use.
8+
var labelEls = document.querySelectorAll('.pdm-label')
9+
10+
// Listen to the color mode and update the UI.
11+
mode.subscribe((m) => labelEls.forEach((el) => (el.textContent = m)))
12+
13+
// At this point our callback will have been called,
14+
// so the text of these will be correct and we can show them.
15+
labelEls.forEach((el) => (el.style.visibility = 'unset'))

packages/docs/components/mode.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//----------------------------------------------------------
2+
// Mode
3+
//----------------------------------------------------------
4+
5+
var { modeSaved, modeOS } = window.__pdm__
6+
7+
// Get some elements we will use.
8+
var modeSavedEls = document.querySelectorAll('.pdm-mode-saved')
9+
var modeOSEls = document.querySelectorAll('.pdm-mode-os')
10+
11+
// Show the saved mode for debugging.
12+
modeSaved.subscribe((v) =>
13+
modeSavedEls.forEach((el) => (el.textContent = `Saved Color Mode: ${v}`)),
14+
)
15+
16+
// Show the OS mode for debugging.
17+
modeOS.subscribe((v) =>
18+
modeOSEls.forEach((el) => (el.textContent = `OS Color Mode: ${v}`)),
19+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//----------------------------------------------------------
2+
// Reset Button
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// Get some elements we will use.
8+
var resetButtonEls = document.querySelectorAll('.pdm-reset')
9+
10+
// These elements will clear the saved color mode,
11+
// which will cause the color mode to fallback to
12+
// the OS color mode.
13+
resetButtonEls.forEach((el) =>
14+
el.addEventListener('click', () => mode.set(undefined)),
15+
)

packages/docs/components/root.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//----------------------------------------------------------
2+
// Root
3+
//----------------------------------------------------------
4+
5+
// We want to make sure that if the OS mode is 'light'
6+
// and the saved mode is 'dark' that we do not transition
7+
// between the two, so we add the transition after a frame
8+
// has passed.
9+
requestAnimationFrame(() =>
10+
requestAnimationFrame(
11+
() =>
12+
(document.documentElement.style.transition =
13+
'background 0.5s, color 0.5s'),
14+
),
15+
)

packages/docs/components/select.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//----------------------------------------------------------
2+
// Select
3+
//----------------------------------------------------------
4+
5+
var { mode, modes } = window.__pdm__
6+
7+
// We can use a select element for choosing a color mode
8+
// instead of a toggle button.
9+
var selectEls = document.querySelectorAll('.pdm-select')
10+
11+
// Update the select options with the modes.
12+
modes.subscribe((modes) => {
13+
selectEls.forEach((el) => {
14+
// Preserve the current selection by saving then re-assigning.
15+
var prevValue = el.value
16+
el.innerHTML = modes
17+
.map((m) => `<option value="${m}">${m}</option>`)
18+
.join('\n')
19+
el.value = prevValue
20+
})
21+
})
22+
23+
// Set the mode on change.
24+
selectEls.forEach((el) =>
25+
el.addEventListener('change', (e) => mode.set(e.target.value)),
26+
)
27+
28+
// Set the value to mode.
29+
mode.subscribe((m) => selectEls.forEach((el) => (el.value = m)))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//----------------------------------------------------------
2+
// Syntax Highlight
3+
//----------------------------------------------------------
4+
5+
var { mode } = window.__pdm__
6+
7+
// Get the color mode links.
8+
var colorModeLinks = document.querySelectorAll('.codestyle')
9+
10+
// Disable color mode links we don't need.
11+
mode.subscribe((m) => {
12+
colorModeLinks.forEach((link) => {
13+
if (!link.className.includes(m)) {
14+
link.disabled = true
15+
} else {
16+
// Enable the link we do need.
17+
link.disabled = false
18+
link.media = 'screen'
19+
}
20+
})
21+
})

0 commit comments

Comments
 (0)