Skip to content

Commit 749525e

Browse files
authored
Docs: Update theme generator (#2963)
* Update theme generator * Invert required color names + fix optional colors * Fix optional colors and add generate button
1 parent 60b4b65 commit 749525e

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/docs/src/routes/(docs)/theme-generator/+page.svelte

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
3434
const generateForegroundColorFrom = function (input, percentage = 0.8) {
3535
const result = interpolate([input, isDark(input) ? "white" : "black"], "oklch")(percentage)
36-
return colorObjToString(result)
36+
return formatHex('oklch(' + colorObjToString(result) + ')')
3737
}
3838
3939
const generateDarkenColorFrom = function (input, percentage = 0.07) {
4040
const result = interpolate([input, "black"], "oklch")(percentage)
41-
return colorObjToString(result)
41+
return formatHex('oklch(' + colorObjToString(result) + ')')
4242
}
4343
4444
function changeColorValuesToObject(input) {
@@ -88,6 +88,8 @@
8888
"error",
8989
]
9090
91+
$: onlyRequiredColorNames = true
92+
9193
$: colors = [
9294
{
9395
name: "primary",
@@ -192,59 +194,46 @@
192194
]
193195
194196
function darken(name, variable, source, percentage = 0.2) {
195-
return {
196-
name: name,
197-
variable: variable,
198-
value: generateDarkenColorFrom(
199-
colors.find((item) => item.name === source).value,
200-
percentage,
201-
"oklch"
202-
),
203-
}
197+
return generateDarkenColorFrom(
198+
colors.find((item) => item.name === source).value,
199+
percentage,
200+
)
204201
}
205202
function contrastMaker(name, variable, source, percentage = 0.8) {
206-
return {
207-
name: name,
208-
variable: variable,
209-
value: generateForegroundColorFrom(
210-
colors.find((item) => item.name === source).value,
211-
percentage,
212-
"oklch"
213-
),
214-
}
203+
return generateForegroundColorFrom(
204+
colors.find((item) => item.name === source).value,
205+
percentage,
206+
)
215207
}
216208
217-
function generateOptionalColors(colors) {
218-
let optionalColors = []
219-
optionalColors.push(darken("base-200", "--b2", "base-100", 0.1))
220-
optionalColors.push(darken("base-300", "--b3", "base-100", 0.2))
221-
optionalColors.push(contrastMaker("base-content", "--bc", "base-100"))
222-
223-
optionalColors.push(contrastMaker("primary-content", "--pc", "primary"))
224-
optionalColors.push(contrastMaker("secondary-content", "--sc", "secondary"))
225-
optionalColors.push(contrastMaker("accent-content", "--ac", "accent"))
226-
optionalColors.push(contrastMaker("neutral-content", "--nc", "neutral"))
227-
228-
optionalColors.push(contrastMaker("info-content", "--inc", "info"))
229-
optionalColors.push(contrastMaker("success-content", "--suc", "success"))
230-
optionalColors.push(contrastMaker("warning-content", "--wac", "warning"))
231-
optionalColors.push(contrastMaker("error-content", "--erc", "error"))
232-
return optionalColors
209+
function generateOptionalColors() {
210+
colors[9].value = darken("base-200", "--b2", "base-100", 0.1)
211+
colors[10].value = darken("base-300", "--b3", "base-100", 0.2)
212+
colors[11].value = contrastMaker("base-content", "--bc", "base-100")
213+
214+
colors[1].value = contrastMaker("primary-content", "--pc", "primary")
215+
colors[3].value = contrastMaker("secondary-content", "--sc", "secondary")
216+
colors[5].value = contrastMaker("accent-content", "--ac", "accent")
217+
colors[7].value = contrastMaker("neutral-content", "--nc", "neutral")
218+
219+
colors[13].value = contrastMaker("info-content", "--inc", "info")
220+
colors[15].value = contrastMaker("success-content", "--suc", "success")
221+
colors[17].value = contrastMaker("warning-content", "--wac", "warning")
222+
colors[19].value = contrastMaker("error-content", "--erc", "error")
233223
}
234224
235225
function generateColors(newColorToCheck = "transparent") {
236226
if (CSS.supports("color", newColorToCheck)) {
227+
if (onlyRequiredColorNames) {
228+
generateOptionalColors()
229+
}
237230
colors
238-
.filter((item) => requiredColorNames.includes(item.name))
239231
.forEach((color) => {
240232
wrapper.style.setProperty(
241233
color.variable,
242234
colorObjToString(toGamut("oklch")(color.value), "oklch")
243235
)
244236
})
245-
generateOptionalColors(colors).forEach((color) => {
246-
wrapper.style.setProperty(color.variable, color.value)
247-
})
248237
if (browser) {
249238
localStorage.setItem("theme-generator-colors", JSON.stringify(colors))
250239
}
@@ -257,6 +246,7 @@
257246
if (browser && localStorage.getItem("theme-generator-colors")) {
258247
localStorage.removeItem("theme-generator-colors")
259248
colors = JSON.parse(localStorage.getItem("theme-generator-default-colors"))
249+
generateOptionalColors()
260250
generateColors()
261251
}
262252
}
@@ -300,6 +290,7 @@
300290
)
301291
//error
302292
// })
293+
generateOptionalColors()
303294
generateColors()
304295
}
305296
@@ -347,6 +338,14 @@
347338
{#if browser}
348339
<div class="mockup-code not-prose relative">
349340
<div class="absolute right-2 top-2">
341+
<button class="btn btn-xs" on:click={() => onlyRequiredColorNames = !onlyRequiredColorNames}>
342+
<Translate text="Show all" />
343+
</button>
344+
{#if !onlyRequiredColorNames}
345+
<button class="btn btn-xs" on:click={() => {generateOptionalColors(); generateColors()}}>
346+
<Translate text="Generate Optional" />
347+
</button>
348+
{/if}
350349
<button class="btn btn-xs" on:click={() => randomize()}>
351350
<Translate text="Randomize" />
352351
</button>
@@ -359,12 +358,12 @@
359358
themes: [
360359
{
361360
mytheme: {`}</code>
362-
{#each colors.filter((item) => requiredColorNames.includes(item.name)) as color}
361+
{#each colors.filter((item) => !onlyRequiredColorNames || requiredColorNames.includes(item.name)) as color}
363362
<code> <span
364363
data-tip="Pick →"
365364
class:tooltip={colors.filter((item) =>
366-
requiredColorNames.includes(item.name)
367-
)[0] == color}
365+
!onlyRequiredColorNames || requiredColorNames.includes(item.name)
366+
)[0] === color}
368367
class="tooltip-open tooltip-accent tooltip-left align-middle"><span
369368
class="inline-block w-1" /><ColorPicker
370369
bind:value={color.value}
@@ -382,7 +381,7 @@
382381
require('daisyui'),
383382
],
384383
//...
385-
}
384+
}
386385
`}</code></pre>
387386
</div>
388387
{/if}

0 commit comments

Comments
 (0)