Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ declare module "@uidotdev/usehooks" {

export function useDocumentTitle(title: string): void;

export function useFavicon(url: string): void;
export function useFavicon(
url: string,
options?: {
temporary?: boolean;
}
): void;

export function useGeolocation(options?: PositionOptions): GeolocationState;

Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,34 @@ export function useDocumentTitle(title) {
}, [title]);
}

export function useFavicon(url) {
export function useFavicon(url, options = {}) {
const optionsRef = React.useRef(options);

React.useEffect(() => {
let link = document.querySelector(`link[rel~="icon"]`);
let created = false;

if (!link) {
link = document.createElement("link");
link.type = "image/x-icon";
link.rel = "icon";
link.href = url;
document.head.appendChild(link);
} else {
link.href = url;
created = true;
}

const original = link.href;
link.href = url;

const { temporary } = optionsRef.current;

if (temporary) {
return () => {
if (created) {
link.remove();
} else {
link.href = original;
}
};
}
}, [url]);
}
Expand Down
9 changes: 5 additions & 4 deletions usehooks.com/src/content/hooks/useFavicon.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import HookDescription from "../../components/HookDescription.astro";
import StaticCodeContainer from "../../components/StaticCodeContainer.astro";

<HookDescription name={frontmatter.name}>
The useFavicon hook is a useful for dynamically update the favicon (shortcut icon) of a web page. By using this hook, you can easily change the favicon by providing a new URL as a parameter. It creates a new link element with the specified URL, sets its type and relationship attributes appropriately, and appends it to the `<head>` section of the document.
The useFavicon hook is useful for dynamically updating the favicon (shortcut icon) of a web page. By using this hook, you can easily change the favicon by providing a new URL as a parameter. It creates a new link element with the specified URL, sets its type and relationship attributes appropriately, and appends it to the `<head>` section of the document. Optionally, you can pass `{ temporary: true }` to automatically restore the original favicon when the component unmounts.
</HookDescription>

<div class="reference">
### Parameters

<div class="table-container">
| Name | Type | Description |
| ---- | ------ | -------------------------------------------------- |
| url | string | The URL of the favicon to be set for the document. |
| Name | Type | Description |
| ------- | ------ | -------------------------------------------------- |
| url | string | The URL of the favicon to be set for the document. |
| options | object | This is an optional configuration object provided when calling `useFavicon`. It currently accepts one property `temporary` which when set to `true`, will restore the original favicon on component unmount. |
</div>
</div>

Expand Down