@unhead/vue 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ import { MaybeComputedRef, MaybeRef } from '@vueuse/shared';
2
+ import * as _unhead_schema from '@unhead/schema';
3
+ import { Title as Title$1, TitleTemplate as TitleTemplate$1, EntryAugmentation, Base as Base$1, Link as Link$1, Meta as Meta$1, Style as Style$1, Script as Script$1, Noscript as Noscript$1, HtmlAttributes as HtmlAttributes$1, BodyAttributes as BodyAttributes$1, MergeHead } from '@unhead/schema';
4
+ import * as unhead from 'unhead';
5
+ import { HeadEntryOptions, Arrayable, HeadClient } from 'unhead';
6
+ import { InjectionKey, Plugin } from 'vue';
7
+
8
+ declare type MaybeComputedRefEntries<T> = MaybeComputedRef<T> | {
9
+ [key in keyof T]?: MaybeComputedRef<T[key]>;
10
+ };
11
+
12
+ declare type Title = MaybeComputedRef<Title$1>;
13
+ declare type TitleTemplate = MaybeRef<TitleTemplate$1> | ((title?: string) => TitleTemplate$1);
14
+ declare type Base<E extends EntryAugmentation = {}> = MaybeComputedRef<MaybeComputedRefEntries<Base$1<E>>>;
15
+ declare type Link<E extends EntryAugmentation = {}> = MaybeComputedRefEntries<Link$1<E>>;
16
+ declare type Meta<E extends EntryAugmentation = {}> = MaybeComputedRefEntries<Meta$1<E>>;
17
+ declare type Style<E extends EntryAugmentation = {}> = MaybeComputedRefEntries<Style$1<E>>;
18
+ declare type Script<E extends EntryAugmentation = {}> = MaybeComputedRefEntries<Script$1<E>>;
19
+ declare type Noscript<E extends EntryAugmentation = {}> = MaybeComputedRefEntries<Noscript$1<E>>;
20
+ declare type HtmlAttributes<E extends EntryAugmentation = {}> = MaybeComputedRef<MaybeComputedRefEntries<HtmlAttributes$1<E>>>;
21
+ declare type BodyAttributes<E extends EntryAugmentation = {}> = MaybeComputedRef<MaybeComputedRefEntries<BodyAttributes$1<E>>>;
22
+ interface ReactiveHead<E extends MergeHead = MergeHead> {
23
+ /**
24
+ * The <title> HTML element defines the document's title that is shown in a browser's title bar or a page's tab.
25
+ * It only contains text; tags within the element are ignored.
26
+ *
27
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
28
+ */
29
+ title?: Title;
30
+ /**
31
+ * Generate the title from a template.
32
+ */
33
+ titleTemplate?: TitleTemplate;
34
+ /**
35
+ * The <base> HTML element specifies the base URL to use for all relative URLs in a document.
36
+ * There can be only one <base> element in a document.
37
+ *
38
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
39
+ */
40
+ base?: Base<E['base']>;
41
+ /**
42
+ * The <link> HTML element specifies relationships between the current document and an external resource.
43
+ * This element is most commonly used to link to stylesheets, but is also used to establish site icons
44
+ * (both "favicon" style icons and icons for the home screen and apps on mobile devices) among other things.
45
+ *
46
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as
47
+ */
48
+ link?: MaybeComputedRef<Link<E['link']>[]>;
49
+ /**
50
+ * The <meta> element represents metadata that cannot be expressed in other HTML elements, like <link> or <script>.
51
+ *
52
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
53
+ */
54
+ meta?: MaybeComputedRef<Meta<E['meta']>[]>;
55
+ /**
56
+ * The <style> HTML element contains style information for a document, or part of a document.
57
+ * It contains CSS, which is applied to the contents of the document containing the <style> element.
58
+ *
59
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style
60
+ */
61
+ style?: MaybeComputedRef<Style<E['style']>[]>;
62
+ /**
63
+ * The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code.
64
+ *
65
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
66
+ */
67
+ script?: MaybeComputedRef<Script<E['script']>[]>;
68
+ /**
69
+ * The <noscript> HTML element defines a section of HTML to be inserted if a script type on the page is unsupported
70
+ * or if scripting is currently turned off in the browser.
71
+ *
72
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript
73
+ */
74
+ noscript?: MaybeComputedRef<Noscript<E['noscript']>[]>;
75
+ /**
76
+ * Attributes for the <html> HTML element.
77
+ *
78
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html
79
+ */
80
+ htmlAttrs?: HtmlAttributes<E['htmlAttrs']>;
81
+ /**
82
+ * Attributes for the <body> HTML element.
83
+ *
84
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body
85
+ */
86
+ bodyAttrs?: BodyAttributes<E['bodyAttrs']>;
87
+ }
88
+
89
+ declare function resolveUnrefHeadInput(ref: any): any;
90
+
91
+ declare const resolveVueInputPlugin: unhead.HeadPlugin<_unhead_schema.Head<_unhead_schema.SchemaAugmentations>>;
92
+
93
+ declare function useServerHead(input: ReactiveHead, options?: HeadEntryOptions): void;
94
+ declare function useHead(input: ReactiveHead, options?: HeadEntryOptions): void;
95
+ declare const useTitle: (title: ReactiveHead['title']) => void;
96
+ declare const useTitleTemplate: (titleTemplate: ReactiveHead['titleTemplate']) => void;
97
+ declare const useMeta: (meta: Arrayable<Meta>) => void;
98
+ declare const useLink: (link: Arrayable<Link>) => void;
99
+ declare const useScript: (script: Arrayable<Script>) => void;
100
+ declare const useStyle: (style: Arrayable<Style>) => void;
101
+ declare const useNoscript: (noscript: Arrayable<Noscript>) => void;
102
+ declare const useBase: (base: ReactiveHead['base']) => void;
103
+ declare const useHtmlAttrs: (attrs: ReactiveHead['htmlAttrs']) => void;
104
+ declare const useBodyAttrs: (attrs: ReactiveHead['bodyAttrs']) => void;
105
+
106
+ declare const Vue3: boolean;
107
+ declare type VueHeadClient = HeadClient<ReactiveHead>;
108
+ declare const headSymbol: InjectionKey<VueHeadClient>;
109
+ declare function injectHead(): VueHeadClient;
110
+ declare function createHead(): HeadClient<ReactiveHead> & Plugin;
111
+
112
+ export { Base, BodyAttributes, HtmlAttributes, Link, MaybeComputedRefEntries, Meta, Noscript, ReactiveHead, Script, Style, Title, TitleTemplate, Vue3, VueHeadClient, createHead, headSymbol, injectHead, resolveUnrefHeadInput, resolveVueInputPlugin, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
package/dist/index.mjs ADDED
@@ -0,0 +1,134 @@
1
+ import { resolveUnref } from '@vueuse/shared';
2
+ import { unref, getCurrentInstance, ref, watchEffect, watch, nextTick, onBeforeUnmount, version, inject } from 'vue';
3
+ import { defineHeadPlugin, asArray, getActiveHead, createHead as createHead$1, hydratesStatePlugin } from 'unhead';
4
+ import { debouncedUpdateDom } from 'unhead/client';
5
+
6
+ function resolveUnrefHeadInput(ref) {
7
+ const root = resolveUnref(ref);
8
+ if (!ref || !root)
9
+ return root;
10
+ if (Array.isArray(root))
11
+ return root.map(resolveUnrefHeadInput);
12
+ if (typeof root === "object") {
13
+ return Object.fromEntries(
14
+ Object.entries(root).map(([key, value]) => {
15
+ if (key === "titleTemplate")
16
+ return [key, unref(value)];
17
+ return [
18
+ key,
19
+ resolveUnrefHeadInput(value)
20
+ ];
21
+ })
22
+ );
23
+ }
24
+ return root;
25
+ }
26
+
27
+ const resolveVueInputPlugin = defineHeadPlugin({
28
+ hooks: {
29
+ "entries:resolve": function({ entries }) {
30
+ for (const k in entries)
31
+ entries[k].input = resolveUnrefHeadInput(entries[k].input);
32
+ }
33
+ }
34
+ });
35
+
36
+ const IS_CLIENT = typeof window !== "undefined";
37
+
38
+ function useHead$2(input, options = {}) {
39
+ const head = injectHead();
40
+ head.push(input, options);
41
+ }
42
+
43
+ function useServerHead$2(input, options = {}) {
44
+ useHead$2(input, { ...options, mode: "server" });
45
+ }
46
+
47
+ function useHead$1(input, options = {}) {
48
+ const head = injectHead();
49
+ const vm = getCurrentInstance();
50
+ if (!vm) {
51
+ head.push(
52
+ input,
53
+ options
54
+ );
55
+ return;
56
+ }
57
+ const sideEffects = [];
58
+ const resolvedInput = ref({});
59
+ watchEffect(() => {
60
+ resolvedInput.value = resolveUnrefHeadInput(input);
61
+ });
62
+ let entry;
63
+ watch(resolvedInput, (e) => {
64
+ if (!entry) {
65
+ entry = head.push(e, options);
66
+ sideEffects.push(() => entry?.dispose());
67
+ } else {
68
+ entry.patch(e);
69
+ debouncedUpdateDom(nextTick, head);
70
+ }
71
+ }, { immediate: true });
72
+ onBeforeUnmount(() => {
73
+ sideEffects.forEach((fn) => fn());
74
+ if (entry)
75
+ entry.dispose();
76
+ debouncedUpdateDom(nextTick, head);
77
+ });
78
+ }
79
+
80
+ function useServerHead$1(input, options = {}) {
81
+ }
82
+
83
+ function useServerHead(input, options = {}) {
84
+ return IS_CLIENT ? useServerHead$1(input, options) : useServerHead$2(input, options);
85
+ }
86
+ function useHead(input, options = {}) {
87
+ return IS_CLIENT ? useHead$1(input, options) : useHead$2(input, options);
88
+ }
89
+ const useTitle = (title) => useHead({ title });
90
+ const useTitleTemplate = (titleTemplate) => useHead({ titleTemplate });
91
+ const useMeta = (meta) => useHead({ meta: asArray(meta) });
92
+ const useLink = (link) => useHead({ link: asArray(link) });
93
+ const useScript = (script) => useHead({ script: asArray(script) });
94
+ const useStyle = (style) => useHead({ style: asArray(style) });
95
+ const useNoscript = (noscript) => useHead({ noscript: asArray(noscript) });
96
+ const useBase = (base) => useHead({ base });
97
+ const useHtmlAttrs = (attrs) => useHead({ htmlAttrs: attrs });
98
+ const useBodyAttrs = (attrs) => useHead({ bodyAttrs: attrs });
99
+
100
+ const Vue3 = version.startsWith("3");
101
+ const headSymbol = Symbol("head");
102
+ function injectHead() {
103
+ return getCurrentInstance() && inject(headSymbol) || getActiveHead();
104
+ }
105
+ function createHead() {
106
+ const head = createHead$1({
107
+ plugins: [
108
+ hydratesStatePlugin,
109
+ resolveVueInputPlugin
110
+ ]
111
+ });
112
+ const vuePlugin = {
113
+ install(app) {
114
+ if (Vue3)
115
+ app.config.globalProperties.$head = head;
116
+ app.provide(headSymbol, head);
117
+ app.mixin({
118
+ created() {
119
+ const instance = getCurrentInstance();
120
+ if (!instance)
121
+ return;
122
+ const options = instance.type;
123
+ if (!options || !("head" in options))
124
+ return;
125
+ const source = typeof options.head === "function" ? () => options.head() : options.head;
126
+ head.push(source);
127
+ }
128
+ });
129
+ }
130
+ };
131
+ return { ...vuePlugin, ...head };
132
+ }
133
+
134
+ export { Vue3, createHead, headSymbol, injectHead, resolveUnrefHeadInput, resolveVueInputPlugin, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
@@ -0,0 +1,14 @@
1
+ import type { Arrayable, HeadEntryOptions } from 'unhead';
2
+ import type { Link, Meta, Noscript, ReactiveHead, Script, Style } from '../../types';
3
+ export declare const useServerHead: (input: ReactiveHead, options?: HeadEntryOptions) => any;
4
+ export declare const useHead: (input: ReactiveHead, options?: HeadEntryOptions) => any;
5
+ export declare const useTitle: (title: ReactiveHead['title']) => any;
6
+ export declare const useTitleTemplate: (titleTemplate: ReactiveHead['titleTemplate']) => any;
7
+ export declare const useMeta: (meta: Arrayable<Meta>) => any;
8
+ export declare const useLink: (link: Arrayable<Link>) => any;
9
+ export declare const useScript: (script: Arrayable<Script>) => any;
10
+ export declare const useStyle: (style: Arrayable<Style>) => any;
11
+ export declare const useNoscript: (noscript: Arrayable<Noscript>) => any;
12
+ export declare const useBase: (base: ReactiveHead['base']) => any;
13
+ export declare const useHtmlAttrs: (attrs: ReactiveHead['htmlAttrs']) => any;
14
+ export declare const useBodyAttrs: (attrs: ReactiveHead['bodyAttrs']) => any;
@@ -0,0 +1,14 @@
1
+ import { asArray } from "unhead";
2
+ import { useHead as _useHead, useServerHead as _useServerHead } from "#head-runtime";
3
+ export const useServerHead = (input, options = {}) => _useServerHead(input, options);
4
+ export const useHead = (input, options = {}) => _useHead(input, options);
5
+ export const useTitle = (title) => useHead({ title });
6
+ export const useTitleTemplate = (titleTemplate) => useHead({ titleTemplate });
7
+ export const useMeta = (meta) => useHead({ meta: asArray(meta) });
8
+ export const useLink = (link) => useHead({ link: asArray(link) });
9
+ export const useScript = (script) => useHead({ script: asArray(script) });
10
+ export const useStyle = (style) => useHead({ style: asArray(style) });
11
+ export const useNoscript = (noscript) => useHead({ noscript: asArray(noscript) });
12
+ export const useBase = (base) => useHead({ base });
13
+ export const useHtmlAttrs = (attrs) => useHead({ htmlAttrs: attrs });
14
+ export const useBodyAttrs = (attrs) => useHead({ bodyAttrs: attrs });
@@ -0,0 +1 @@
1
+ export * from './composables';
@@ -0,0 +1 @@
1
+ export * from "./composables.mjs";
@@ -0,0 +1,2 @@
1
+ export * from './useHead';
2
+ export * from './useServerHead';
@@ -0,0 +1,2 @@
1
+ export * from "./useHead.mjs";
2
+ export * from "./useServerHead.mjs";
@@ -0,0 +1,3 @@
1
+ import type { HeadEntryOptions } from 'unhead';
2
+ import type { ReactiveHead } from '../../types';
3
+ export declare function useHead(input: ReactiveHead, options?: HeadEntryOptions): void;
@@ -0,0 +1,5 @@
1
+ import { injectHead } from "../..";
2
+ export function useHead(input, options = {}) {
3
+ const head = injectHead();
4
+ head.push(input, options);
5
+ }
@@ -0,0 +1,3 @@
1
+ import type { HeadEntryOptions } from 'unhead';
2
+ import type { ReactiveHead } from '../../types';
3
+ export declare function useServerHead(input: ReactiveHead, options?: HeadEntryOptions): void;
@@ -0,0 +1,4 @@
1
+ import { useHead } from "./index.mjs";
2
+ export function useServerHead(input, options = {}) {
3
+ useHead(input, { ...options, mode: "server" });
4
+ }
@@ -0,0 +1,2 @@
1
+ export * from './useHead';
2
+ export * from './useServerHead';
@@ -0,0 +1,2 @@
1
+ export * from "./useHead.mjs";
2
+ export * from "./useServerHead.mjs";
@@ -0,0 +1,3 @@
1
+ import type { HeadEntryOptions } from 'unhead';
2
+ import type { ReactiveHead } from '../../types';
3
+ export declare function useHead(input: ReactiveHead, options?: HeadEntryOptions): void;
@@ -0,0 +1,35 @@
1
+ import { getCurrentInstance, nextTick, onBeforeUnmount, ref, watch, watchEffect } from "vue";
2
+ import { debouncedUpdateDom } from "unhead/client";
3
+ import { injectHead, resolveUnrefHeadInput } from "../../index";
4
+ export function useHead(input, options = {}) {
5
+ const head = injectHead();
6
+ const vm = getCurrentInstance();
7
+ if (!vm) {
8
+ head.push(
9
+ input,
10
+ options
11
+ );
12
+ return;
13
+ }
14
+ const sideEffects = [];
15
+ const resolvedInput = ref({});
16
+ watchEffect(() => {
17
+ resolvedInput.value = resolveUnrefHeadInput(input);
18
+ });
19
+ let entry;
20
+ watch(resolvedInput, (e) => {
21
+ if (!entry) {
22
+ entry = head.push(e, options);
23
+ sideEffects.push(() => entry?.dispose());
24
+ } else {
25
+ entry.patch(e);
26
+ debouncedUpdateDom(nextTick, head);
27
+ }
28
+ }, { immediate: true });
29
+ onBeforeUnmount(() => {
30
+ sideEffects.forEach((fn) => fn());
31
+ if (entry)
32
+ entry.dispose();
33
+ debouncedUpdateDom(nextTick, head);
34
+ });
35
+ }
@@ -0,0 +1,3 @@
1
+ import type { HeadEntryOptions } from 'unhead';
2
+ import type { ReactiveHead } from '../../types';
3
+ export declare function useServerHead(input: ReactiveHead, options?: HeadEntryOptions): void;
@@ -0,0 +1,2 @@
1
+ export function useServerHead(input, options = {}) {
2
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@unhead/vue",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "packageManager": "pnpm@7.14.0",
6
+ "author": "Harlan Wilton <harlan@harlanzw.com>",
7
+ "license": "MIT",
8
+ "funding": "https://github.com/sponsors/harlan-zw",
9
+ "homepage": "https://github.com/harlan-zw/unhead#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/harlan-zw/unhead.git",
13
+ "directory": "packages/vue"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/harlan-zw/unhead/issues"
17
+ },
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.mjs"
23
+ }
24
+ },
25
+ "module": "dist/index.mjs",
26
+ "types": "dist/index.d.ts",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "peerDependencies": {
31
+ "vue": ">=2.7 || >=3"
32
+ },
33
+ "dependencies": {
34
+ "@unhead/schema": "0.0.1",
35
+ "@vueuse/shared": "latest",
36
+ "unhead": "0.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "vue": "latest"
40
+ },
41
+ "scripts": {
42
+ "build": "unbuild .",
43
+ "stub": "unbuild . --stub"
44
+ }
45
+ }