-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
149 lines (132 loc) · 5.01 KB
/
index.html
File metadata and controls
149 lines (132 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>settings-panel</title>
<script type="importmap">
{
"imports": {
"sprae": "https://esm.sh/sprae@12",
"sprae/store": "https://esm.sh/sprae@12/store"
}
}
</script>
<!-- <script type="importmap">
{
"imports": {
"sprae": "../node_modules/sprae/sprae.js",
"sprae/store": "../node_modules/sprae/store.js"
}
}
</script> -->
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-family: system-ui;
/* Derive from panel's --bg, darken by 0.1 L */
background: oklch(from var(--bg, oklch(.96 0 0)) calc(l - 0.1) c h);
}
</style>
</head>
<body>
<div id="panel"></div>
<script type="module">
import settings from './index.js'
import base from './theme/default.js'
import skeu from './theme/skeu.js'
import control from './theme/control-panel.js'
const themes = { default: base, skeu, control }
const axes = {
default: ['shade', 'accent', 'spacing', 'weight', 'roundness'],
skeu: ['shade', 'accent', 'spacing', 'weight', 'roundness', 'bevel', 'depth', 'grid'],
}
const paramKeys = ['shade', 'accent', 'weight', 'depth', 'roundness', 'spacing', 'bevel', 'grid']
const getParams = (s) => Object.fromEntries(paramKeys.map(k => [k, s[k]]))
const rand = (lo, hi, step = 0) => {
const v = lo + Math.random() * (hi - lo)
return step ? Math.round(v / step) * step : v
}
const randHex = () => '#' + Array.from({ length: 3 }, () => Math.floor(Math.random() * 256).toString(16).padStart(2, '0')).join('')
const schema = {
foldable: false,
theme: { type: 'select', options: Object.keys(themes), value: 'skeu' },
'params.shade': '#f7f7f7',
'params.accent': '#2967fa',
'params.weight': { type: 'slider', value: 400, min: 100, max: 900, step: 50, marks: true, snap: true },
'params.bevel': { type: 'slider', value: 1, min: 0, max: 2, step: 0.1, marks: true },
'params.depth': { type: 'slider', value: 1, min: 0, max: 2, step: 0.1, marks: true },
'params.roundness': { type: 'slider', value: 1, min: 0, max: 2, marks: true, step: 0.25 },
'params.spacing': { type: 'slider', value: 1, min: 0.5, max: 2, step: 0.25, marks: true },
'params.grid': { type: 'select', variant: 'segmented', multiple: true, options: ['dots', 'lines', 'crosses'], value: [] },
code: { type: 'textarea', variant: 'code', label: '', value: '', readonly: true },
actions: { type: 'button', label: '', variant: 'group', buttons: {
'Random': { variant: 'secondary', onclick: () => randomize() },
'Copy': () => navigator.clipboard.writeText(state?.code)
}}
}
let state
let curTheme, curFoldable
function create() {
state?.[Symbol.dispose]()
state = settings(schema, {
title: 'Settings',
collapsed: (s) => s.foldable ? false : undefined,
container: '#panel',
theme: (s) => { const t = themes[s.theme]; return typeof t === 'function' ? t(getParams(s)) : t },
persist: 'settings-panel',
onchange(s) {
s.code = JSON.stringify(getParams(s), null, 2)
if (s.theme !== curTheme || s.foldable !== curFoldable) {
curTheme = s.theme
curFoldable = s.foldable
requestAnimationFrame(create)
return
}
syncBg()
}
})
curTheme = state.theme
curFoldable = state.foldable
state.code = JSON.stringify(getParams(state), null, 2)
syncBg()
syncAxes(curTheme)
}
create()
function randomize() {
if (!state) return
state.shade = randHex()
state.accent = randHex()
state.weight = rand(100, 900, 100)
state.bevel = rand(0, 2, 0.1)
state.depth = rand(0, 2, 0.1)
state.roundness = rand(0, 2, 0.25)
state.spacing = rand(0.5, 2, 0.25)
}
function syncBg() {
const el = document.querySelector('.s-panel')
if (!el) return
const bg = getComputedStyle(el).getPropertyValue('--bg')
if (bg) document.documentElement.style.setProperty('--bg', bg)
}
function syncAxes(theme) {
const supported = axes[theme]
const panel = document.querySelector('.s-panel')
if (!panel) return
for (const el of panel.querySelectorAll('[data-key]')) {
const key = el.dataset.key
if (!paramKeys.includes(key)) continue
const input = el.querySelector('.s-input')
if (input) input.disabled = supported ? !supported.includes(key) : false
}
}
</script>
</body>
</html>