Skip to content

Commit

Permalink
add theme-color meta tag if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ollema committed Jan 25, 2024
1 parent e63d34c commit f4fe320
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 27 deletions.
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@ To enable this, set the `themeColor` prop to your preferred colors:
<ModeWatcher themeColor={{ dark: "black", light: "white" }}>
```

Note that for this to work, you must have added the `theme-color` meta tag to your `head` element in `app.html`:

```diff
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
+ <meta name="theme-color" content="black" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div>%sveltekit.body%</div>
</body>
</html>
```

## API

### toggleMode
Expand Down
1 change: 0 additions & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="theme-color" content="black" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
Expand Down
6 changes: 6 additions & 0 deletions src/lib/mode-watcher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
</script>

<svelte:head>
{#if themeColors}
<!-- default to dark mode for to allow testing -->
<!-- this will be overwritten by FOUC prevention snippet below -->
<!-- but that snippet does not run in vitest -->
<meta name="theme-color" content={themeColors.dark} />
{/if}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html `<script nonce="%sveltekit.nonce%">(` + stringified + `)(` + args + `);</script>`}
</svelte:head>
4 changes: 0 additions & 4 deletions src/tests/Mode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
export let track = true;
</script>

<svelte:head>
<meta name="theme-color" content="black" />
</svelte:head>

<ModeWatcher {track} themeColors={{ dark: 'black', light: 'white' }} />
<span data-testid="mode">{$mode}</span>
<button on:click={toggleMode} data-testid="toggle"> toggle </button>
Expand Down
4 changes: 0 additions & 4 deletions src/tests/StealthMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
export let track = true;
</script>

<svelte:head>
<meta name="theme-color" content="black" />
</svelte:head>

<ModeWatcher {track} themeColors={{ dark: 'black', light: 'white' }} />
<button on:click={toggleMode} data-testid="toggle"> toggle </button>
<button on:click={() => setMode('light')} data-testid="light">light</button>
Expand Down

0 comments on commit f4fe320

Please sign in to comment.