Skip to content

Commit 18be006

Browse files
authored
feat(ThemeSwitch) Allow custom classes for icon (#525)
* Allow custom icon colors in ThemeSwitcher * prettier * Support global ThemeSwitch class settings * Use dynamic color class * format * doc on wrong method * Add changeset * Change structure of settings obj
1 parent e1fc930 commit 18be006

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

.changeset/beige-pugs-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-ux': minor
3+
---
4+
5+
Allow custom styling of ThemeSwitch component

packages/svelte-ux/src/lib/components/ThemeSwitch.svelte

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55
import Icon from './Icon.svelte';
66
77
import { getSettings } from './settings.js';
8+
import { cls } from '../utils/index.js';
9+
import type { ComponentProps } from 'svelte';
10+
import { clsMerge } from '$lib/utils/styles.js';
11+
import { getComponentClasses } from '$lib/components/theme.js';
812
913
const { currentTheme } = getSettings();
14+
15+
const { icon: iconClasses, ...otherClasses } = getComponentClasses('ThemeSwitch');
16+
17+
export let classes: {
18+
icon?: string;
19+
} & ComponentProps<Switch>['classes'] = {};
1020
</script>
1121

1222
<Switch
@@ -16,22 +26,34 @@
1626
let newTheme = e.target?.checked ? 'dark' : 'light';
1727
currentTheme.setTheme(newTheme);
1828
}}
19-
classes={{
20-
switch: 'dark:bg-primary dark:border-primary',
21-
toggle: 'translate-x-0 dark:translate-x-full',
22-
}}
29+
classes={clsMerge(
30+
{
31+
switch: 'dark:bg-primary dark:border-primary',
32+
toggle: 'translate-x-0 dark:translate-x-full',
33+
},
34+
otherClasses,
35+
classes
36+
)}
2337
{...$$restProps}
2438
>
2539
<div class="grid grid-cols-1 grid-rows-1">
2640
<Icon
2741
data={mdiWeatherNight}
2842
size=".8rem"
29-
class="row-[1] col-[1] text-primary opacity-0 dark:opacity-100"
43+
class={cls(
44+
'row-[1] col-[1] text-primary opacity-0 dark:opacity-100',
45+
iconClasses,
46+
classes.icon
47+
)}
3048
/>
3149
<Icon
3250
data={mdiWhiteBalanceSunny}
3351
size=".8rem"
34-
class="row-[1] col-[1] text-primary opacity-100 dark:opacity-0"
52+
class={cls(
53+
'row-[1] col-[1] text-primary opacity-100 dark:opacity-0',
54+
iconClasses,
55+
classes.icon
56+
)}
3557
/>
3658
</div>
3759
</Switch>

packages/svelte-ux/src/lib/components/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ export function resolveComponentSettings<NAME extends ComponentName>(
165165
return output;
166166
}
167167

168+
/**
169+
* Returns default component props and classes for a given component.
170+
* @param name component name
171+
*/
168172
export function getComponentSettings<NAME extends ComponentName>(
169173
name: NAME
170174
): ResolvedComponentSettings<NAME> {

packages/svelte-ux/src/lib/components/theme.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export function resolveComponentClasses<NAME extends ComponentName>(
121121
return typeof theme === 'string' ? { root: theme } : (theme ?? {});
122122
}
123123

124+
/**
125+
* Returns default component classes for a given component. See {@link resolveComponentSettings}
126+
* to get both default props and classes.
127+
* @param name component name
128+
*/
124129
export function getComponentClasses<NAME extends ComponentName>(
125130
name: NAME
126131
): ResolvedComponentClasses[NAME] {

packages/svelte-ux/src/routes/docs/components/ThemeSwitch/+page.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@
1010
<Preview>
1111
<ThemeSwitch />
1212
</Preview>
13+
14+
<h2>Customize Switch</h2>
15+
16+
<Preview>
17+
<ThemeSwitch
18+
classes={{ icon: 'text-primary-content', switch: 'bg-secondary w-20', toggle: 'bg-accent' }}
19+
/>
20+
</Preview>

0 commit comments

Comments
 (0)