Skip to content

Commit

Permalink
feat: placeholder slot for component (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Chopin <seb@nuxt.com>
Co-authored-by: Sébastien Chopin <seb@nuxtlabs.com>
  • Loading branch information
3 people committed Sep 13, 2024
1 parent c4df5d9 commit 2094c6b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
7 changes: 5 additions & 2 deletions docs/content/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ The current version of `@nuxtjs/color-mode` is compatible with [Nuxt 3 and Nuxt
::

Add `@nuxtjs/color-mode` dependency to your project:

```bash
npx nuxi@latest module add color-mode
npx nuximodule add color-mode
```

Then, add `@nuxtjs/color-mode` to the `modules` section of your `nuxt.config.ts`
Expand All @@ -42,7 +43,6 @@ Then, add `@nuxtjs/color-mode` to the `modules` section of your `nuxt.config.ts`
export default defineNuxtConfig({
modules: [
'@nuxtjs/color-mode'
// '@nuxtjs/tailwindcss'
]
})
```
Expand Down Expand Up @@ -167,6 +167,9 @@ Props:
- `placeholder`: `String`
- `tag`: `String`, default: `'span'`

Slots:
- `placeholder`: used to render as placeholder, similar to the `placeholder` prop

## Migrating to v3

v3 of `@nuxtjs/color-mode` requires either Nuxt Bridge or Nuxt 3. (If you are using Nuxt 2 without Bridge, you should continue to use v2.)
Expand Down
13 changes: 12 additions & 1 deletion src/runtime/component.vue2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ export default {
default: 'span',
},
},
render(createElement, { data, props, children }) {
render(createElement, { data, props, children, slots }) {
// transform props for <client-only>
if (slots.placeholder) {
props = {
placeholderTag: props.placeholderTag,
}
const scopedSlots = {
placeholder: () => slots.placeholder(),
}
return createElement('client-only', { ...data, props, scopedSlots }, children)
}
props = {
placeholder: props.placeholder,
placeholderTag: props.tag,
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/component.vue3.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ export default {
:placeholder-tag="tag"
>
<slot />
<template
v-if="!placeholder"
#fallback
>
<slot name="placeholder" />
</template>
</ClientOnly>
</template>
2 changes: 1 addition & 1 deletion src/script.min.js

0 comments on commit 2094c6b

Please sign in to comment.