Skip to content
On this page

useFavicon

Category
Export Size
930 B
Last Changed
6 months ago

Reactive favicon

Demo

Change favicon to

Usage

js
import { useFavicon } from '@vueuse/core'

const icon = useFavicon()

icon.value = 'dark.png' // change current icon
import { useFavicon } from '@vueuse/core'

const icon = useFavicon()

icon.value = 'dark.png' // change current icon

Passing a source ref

You can pass a ref to it, changes from of the source ref will be reflected to your favicon automatically.

js
import { computed } from 'vue'
import { useFavicon, usePreferredDark } from '@vueuse/core'

const isDark = usePreferredDark()
const favicon = computed(() => isDark.value ? 'dark.png' : 'light.png')

useFavicon(favicon)
import { computed } from 'vue'
import { useFavicon, usePreferredDark } from '@vueuse/core'

const isDark = usePreferredDark()
const favicon = computed(() => isDark.value ? 'dark.png' : 'light.png')

useFavicon(favicon)

When a source ref is passed, the return ref will be identical to the source ref

ts
const source = ref('icon.png')
const icon = useFavicon(source)

console.log(icon === source) // true
const source = ref('icon.png')
const icon = useFavicon(source)

console.log(icon === source) // true

Type Declarations

typescript
export interface UseFaviconOptions extends ConfigurableDocument {
  baseUrl?: string
  rel?: string
}
/**
 * Reactive favicon.
 *
 * @see https://vueuse.org/useFavicon
 * @param newIcon
 * @param options
 */
export declare function useFavicon(
  newIcon: MaybeReadonlyRef<string | null | undefined>,
  options?: UseFaviconOptions
): ComputedRef<string | null | undefined>
export declare function useFavicon(
  newIcon?: MaybeRef<string | null | undefined>,
  options?: UseFaviconOptions
): Ref<string | null | undefined>
export type UseFaviconReturn = ReturnType<typeof useFavicon>
export interface UseFaviconOptions extends ConfigurableDocument {
  baseUrl?: string
  rel?: string
}
/**
 * Reactive favicon.
 *
 * @see https://vueuse.org/useFavicon
 * @param newIcon
 * @param options
 */
export declare function useFavicon(
  newIcon: MaybeReadonlyRef<string | null | undefined>,
  options?: UseFaviconOptions
): ComputedRef<string | null | undefined>
export declare function useFavicon(
  newIcon?: MaybeRef<string | null | undefined>,
  options?: UseFaviconOptions
): Ref<string | null | undefined>
export type UseFaviconReturn = ReturnType<typeof useFavicon>

Source

SourceDemoDocs

Contributors

Anthony Fu
Antério Vieira
azaleta
Mikhailov Nikita
Jelf
Alex Kozack

Changelog

v9.2.0 on 9/5/2022
df718 - fix: improve type overload (#2123)
v9.1.0 on 8/4/2022
06e26 - fix: writable return type (#2036)
v8.9.3 on 7/14/2022
99e07 - fix!: rename type FaviconOptions to UseFaviconOptions (#1865)
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)

Released under the MIT License.