-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
106 additions
and
21 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,12 @@ | ||
```tsx | ||
const [favicon, setFavicon] = createSignal('https://docs.solidjs.com/favicon.svg'); | ||
const setXFavicon = () => setFavicon('https://x.com/favicon.ico'); | ||
const setSolidFavicon = () => setFavicon('https://docs.solidjs.com/favicon.svg'); | ||
|
||
useFavicon(favicon); | ||
|
||
return ( | ||
<button onClick={setXFavicon}>Use X Favicon</button> | ||
<button onClick={setSolidFavicon}>Use SolidJS Favicon</button> | ||
) | ||
``` |
41 changes: 41 additions & 0 deletions
41
dev/components/examples/use-favicon/use-favicon.example.tsx
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,41 @@ | ||
import { useFavicon } from 'src'; | ||
import { ExampleBase } from '../example-base'; | ||
import Code from './use-favicon.code.mdx'; | ||
|
||
import { createSignal } from 'solid-js'; | ||
import { useMDXComponents } from 'solid-jsx'; | ||
|
||
export function UseFaviconExample() { | ||
const [favicon, setFavicon] = createSignal('https://docs.solidjs.com/favicon.svg'); | ||
const setXFavicon = () => setFavicon('https://x.com/favicon.ico'); | ||
const setSolidFavicon = () => setFavicon('https://docs.solidjs.com/favicon.svg'); | ||
|
||
useFavicon(favicon); | ||
|
||
// @ts-ignore | ||
const components: any = useMDXComponents(); | ||
|
||
return ( | ||
<ExampleBase | ||
title="useFavicon" | ||
description="Appends <link /> element to head component with given favicon url. The hook is not called during server side rendering." | ||
code={<Code components={components} />} | ||
> | ||
<div class="flex h-full w-full flex-col items-center justify-center gap-3 rounded-md border p-3 py-10 text-center text-sm"> | ||
<button | ||
onClick={() => setXFavicon()} | ||
class="rounded-md border px-2 py-1.5 transition active:scale-95" | ||
> | ||
X favicon | ||
</button> | ||
|
||
<button | ||
onClick={() => setSolidFavicon()} | ||
class="rounded-md border px-2 py-1.5 transition active:scale-95" | ||
> | ||
Solid favicon | ||
</button> | ||
</div> | ||
</ExampleBase> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Accessor, createEffect, createSignal } from 'solid-js'; | ||
|
||
const MIME_TYPES: Record<string, string> = { | ||
ico: 'image/x-icon', | ||
png: 'image/png', | ||
svg: 'image/svg+xml', | ||
gif: 'image/gif', | ||
}; | ||
|
||
/** | ||
* A hook that sets the favicon of the page based on the url (accessor) passed. | ||
*/ | ||
export function useFavicon(url: Accessor<string>) { | ||
const [linkRef, setLinkRef] = createSignal<HTMLLinkElement>(); | ||
|
||
createEffect(() => { | ||
if (!url()) { | ||
return; | ||
} | ||
|
||
if (!linkRef()) { | ||
const existingElements = document.querySelectorAll<HTMLLinkElement>('link[rel*="icon"]'); | ||
existingElements.forEach(element => document.head.removeChild(element)); | ||
|
||
const element = document.createElement('link'); | ||
element.rel = 'shortcut icon'; | ||
setLinkRef(element); | ||
document.querySelector('head')!.appendChild(element); | ||
} | ||
|
||
const splittedUrl = url().split('.'); | ||
linkRef()?.setAttribute( | ||
'type', | ||
MIME_TYPES[splittedUrl?.[splittedUrl.length - 1]?.toLowerCase()!]!, | ||
); | ||
linkRef()?.setAttribute('href', url()); | ||
}); | ||
} |