-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow automatic injection of head()
into components
#43
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9527fc6
feat: allow automatic injection of `head()` into components (POC)
danielroe 7bacc5e
fix: respect possible object `head`
danielroe f580f6d
perf: only inject helper if there is a `head` property
danielroe 25ec719
fix: pass `{}` to enable `useMeta` helper
danielroe e134dbb
fix: add explanatory text if component doesn't have head
danielroe aa788ea
Merge branch 'master' into component-redefinition
danielroe 301b3f0
test: assert type equivalence of `defineComponent`
danielroe 0c50ee6
fix: improve type safety and access existing head
danielroe 4ddbcd6
docs: update docs
danielroe a15fd9d
Merge branch 'master' into component-redefinition
danielroe 65551af
docs: give another example
danielroe 5e70db7
Merge branch 'master' into component-redefinition
danielroe 80202ca
Merge branch 'master' into component-redefinition
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'defu' | ||
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
--- | ||
--- | ||
|
||
# useMeta | ||
|
||
You can interact directly with [`head()` properties](https://nuxtjs.org/api/pages-head/) in `setup` by means of the `useMeta()` helper. | ||
|
||
```ts | ||
import { defineComponent, useMeta, computed } from 'nuxt-composition-api' | ||
|
||
export default defineComponent({ | ||
// You need to define an empty head to activate this functionality | ||
head: {}, | ||
setup() { | ||
// This will allow you to set the title in head - but won't allow you to read its state outside of this component. | ||
const { title } = useMeta() | ||
title.value = 'My page' | ||
|
||
// You could also provide an initial value. | ||
const { title } = useMeta({ title: 'My page' }) | ||
|
||
// ... or simply set some meta tags | ||
useMeta({ title: 'My page', ... }) | ||
}, | ||
}) | ||
``` |
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,53 @@ | ||
import { | ||
defineComponent as define, | ||
reactive, | ||
toRefs, | ||
getCurrentInstance, | ||
} from '@vue/composition-api' | ||
import defu from 'defu' | ||
import { MetaInfo } from 'vue-meta' | ||
|
||
import { createEmptyMeta } from './meta' | ||
|
||
import { UnwrapRef, Ref } from '@vue/composition-api/dist/reactivity' | ||
|
||
type ReactiveHead<T = {}> = UnwrapRef<Ref<MetaInfo & T>> | ||
|
||
export const defineComponent: typeof define = (options: any) => { | ||
if (!('head' in options)) return options | ||
|
||
const _head: ReactiveHead = reactive<MetaInfo>({}) | ||
if (!(options.head instanceof Function)) { | ||
Object.assign(_head, options.head) | ||
} | ||
const head = | ||
options.head instanceof Function | ||
? () => defu(_head, options.head()) | ||
: () => _head | ||
|
||
return { | ||
...options, | ||
_head, | ||
head, | ||
} | ||
} | ||
|
||
export const useMeta = <T extends MetaInfo>(init?: T) => { | ||
const vm = getCurrentInstance() | ||
if (!vm) throw new Error('useMeta must be called within a component.') | ||
|
||
if (!('_head' in vm.$options)) | ||
throw new Error( | ||
'In order to enable `useMeta`, please make sure you include `head: {}` within your component definition, and you are using the `defineComponent` exported from nuxt-composition-api.' | ||
) | ||
|
||
const { _head } = vm.$options as { _head: ReactiveHead } | ||
|
||
Object.assign(_head, createEmptyMeta()) | ||
Object.assign(_head, init || {}) | ||
return toRefs( | ||
_head as UnwrapRef< | ||
Ref<Exclude<ReturnType<typeof createEmptyMeta>, keyof T> & T> | ||
> | ||
) | ||
} |
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,6 @@ | ||
import { expectType } from 'tsd' | ||
import { defineComponent } from '@vue/composition-api' | ||
|
||
import { defineComponent as modifiedDefineComponent } from '../..' | ||
|
||
expectType<typeof defineComponent>(modifiedDefineComponent) |
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,6 @@ | ||
import { expectType } from 'tsd' | ||
|
||
import { useMeta, Ref } from '../..' | ||
|
||
expectType<Ref<string>>(useMeta({ title: 'provided' }).title) | ||
expectType<Ref<string | undefined>>(useMeta().title) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to go.
defu
has types but for some reason rollup wasn't recognising them until I edited itspackage.json
.unjs/defu#15