-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ThemeToggleButton, tests and doc (#482)
# Motivation Add new ThemeToggleButton component # Changes ThemeToggleButton component added to repo Tests for ThemeToggleButton added to spec file ThemeToggleButton is added to showcase --icon-color variable added to IconDarkMode and IconLightMode New --sidebar-icon variable has been added to themes # Screenshots <img width="1325" alt="Screenshot 2024-09-17 at 14 16 49" src="https://github.com/user-attachments/assets/9f142485-467c-4561-a3ae-638662fb724d"> <img width="1327" alt="Screenshot 2024-09-17 at 14 15 09" src="https://github.com/user-attachments/assets/a4ba4458-ca81-43ed-8d0c-b231dc63ec58"> --------- Co-authored-by: “Cosku <“cosku.cinkilic@dfinity.org”>
- Loading branch information
1 parent
eafc935
commit 9a16f6f
Showing
9 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<script lang="ts"> | ||
import { Theme } from "$lib/types/theme"; | ||
import { themeStore } from "$lib/stores/theme.store"; | ||
import { i18n } from "$lib/stores/i18n"; | ||
import IconLightMode from "$lib/icons/IconLightMode.svelte"; | ||
import IconDarkMode from "$lib/icons/IconDarkMode.svelte"; | ||
import { fade } from "svelte/transition"; | ||
const switchTheme = () => { | ||
themeStore.select($themeStore === Theme.LIGHT ? Theme.DARK : Theme.LIGHT); | ||
}; | ||
let isDarkMode: boolean; | ||
$: isDarkMode = $themeStore === Theme.DARK; | ||
</script> | ||
|
||
<button | ||
data-tid="theme-toggle-button" | ||
class="theme-toggle-button icon-only" | ||
on:click={switchTheme} | ||
aria-label={$i18n.theme.switch_theme} | ||
> | ||
{#if isDarkMode} | ||
<span in:fade|global> | ||
<IconLightMode /> | ||
</span> | ||
{:else} | ||
<span in:fade|global> | ||
<IconDarkMode /> | ||
</span> | ||
{/if} | ||
</button> | ||
|
||
<style lang="scss"> | ||
.theme-toggle-button { | ||
// Height and width base on icon size + padding | ||
// To fix the issue of ThemeToggle being stretched by parents | ||
height: calc(var(--padding-2x) + 20px); | ||
width: calc(var(--padding-2x) + 20px); | ||
color: var(--sidebar-icon); | ||
padding: var(--padding); | ||
background: var(--sidebar-button-background); | ||
line-height: 0; | ||
&:hover { | ||
background: var(--sidebar-button-background-hover); | ||
--icon-fill: var(--sidebar-icon); | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/routes/(split)/components/theme-toggle-button/+page.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script lang="ts"> | ||
import ThemeToggleButton from "$lib/components/ThemeToggleButton.svelte"; | ||
</script> | ||
|
||
# Theme Toggle Button | ||
|
||
In addition to [theming](/styling/theming) we also provide an opinionated toggle to integrate easily a theme switcher in your dapp. | ||
|
||
```javascript | ||
<ThemeToggleButton /> | ||
``` | ||
|
||
## Showcase | ||
|
||
<ThemeToggleButton /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import ThemeToggleButton from "$lib/components/ThemeToggleButton.svelte"; | ||
import IconDarkMode from "$lib/icons/IconDarkMode.svelte"; | ||
import IconLightMode from "$lib/icons/IconLightMode.svelte"; | ||
import { themeStore } from "$lib/stores/theme.store"; | ||
import { Theme } from "$lib/types/theme"; | ||
import { fireEvent, render } from "@testing-library/svelte"; | ||
import { get } from "svelte/store"; | ||
import en from "../mocks/i18n.mock"; | ||
|
||
describe("ThemeToggleButton", () => { | ||
it("should render a toggle button", () => { | ||
const { container } = render(ThemeToggleButton); | ||
|
||
const input = container.querySelector("button") as HTMLButtonElement; | ||
expect(input).not.toBeNull(); | ||
}); | ||
|
||
it("should render an accessible toggle button", () => { | ||
const { container } = render(ThemeToggleButton); | ||
|
||
const input = container.querySelector("button") as HTMLButtonElement; | ||
expect(input.getAttribute("aria-label")).toEqual(en.theme.switch_theme); | ||
}); | ||
|
||
it("should switch theme", () => { | ||
const { container } = render(ThemeToggleButton); | ||
|
||
const input = container.querySelector("button") as HTMLButtonElement; | ||
|
||
fireEvent.click(input); | ||
expect(get(themeStore)).toEqual(Theme.LIGHT); | ||
|
||
fireEvent.click(input); | ||
expect(get(themeStore)).toEqual(Theme.DARK); | ||
}); | ||
|
||
it("should render IconLightMode when theme is dark", () => { | ||
themeStore.select(Theme.DARK); | ||
const { container } = render(ThemeToggleButton); | ||
|
||
const lightModeIcon = container.querySelector("svg"); | ||
expect(lightModeIcon).toBeInstanceOf(SVGSVGElement); | ||
expect(container.innerHTML).toContain( | ||
render(IconLightMode).container.innerHTML, | ||
); | ||
}); | ||
|
||
it("should render IconDarkMode when theme is light", () => { | ||
themeStore.select(Theme.LIGHT); | ||
const { container } = render(ThemeToggleButton); | ||
|
||
const darkModeIcon = container.querySelector("svg"); | ||
expect(darkModeIcon).toBeInstanceOf(SVGSVGElement); | ||
expect(container.innerHTML).toContain( | ||
render(IconDarkMode).container.innerHTML, | ||
); | ||
}); | ||
}); |