Skip to content

Commit 4cc21b2

Browse files
alexieremiafago
andauthored
improve: MCD-474: Update typing and content attribute usage (#116)
* improve: MCD-474: Update typing * MCD-474: Add comment (#117) * MCD-474: Update docs * MCD-474: Remove type import * MCD-474: Improve docs to include types * Update 30.render-custom-elements.md --------- Co-authored-by: Wolfgang Ziegler <[email protected]>
1 parent ee59f66 commit 4cc21b2

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

content/1.get-started/10.how-it-works.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,24 @@ The following example shows how Vue components for the previous custom elements
149149
<div class="teaser-listing">
150150
<h2>Title: {{ title }}</h2>
151151
<slot>
152-
<component :is="useDrupalCe().renderCustomElements($attrs.content)" />
152+
<component :is="useDrupalCe().renderCustomElements(content)" />
153153
</slot>
154154
</div>
155155
</template>
156156
157157
<script setup lang="ts">
158158
defineSlots<{
159-
default();
160-
}>()
159+
default(): any;
160+
}>();
161161
defineProps<{
162-
title: String;
162+
title: string;
163+
content?: CustomElementContent;
163164
}>();
164165
</script>
165166
```
166167
::
167168

168-
The above example of `TeaserListing.vue` makes use of slot content, which should contain our rendered article teasers. Vue.js renders contained markup instead of the `<slot>` element when using `markup`-based rendering. For rendering the JSON serialization of custom elements (which is used by default), the slot value is passed to the component as data via `$attrs.content`. This data gets rendered with the help of the `renderCustomElements()` utility, when no slot content is passed to the component. Thus, this way the slot renders correctly for both JSON and markup based rendering. Refer to [Render custom elements](/nuxt/render-custom-elements) docs for more details on that.
169+
The above example of `TeaserListing.vue` makes use of slot content, which should contain our rendered article teasers. Vue.js renders contained markup instead of the `<slot>` element when using `markup`-based rendering. For rendering the JSON serialization of custom elements (which is used by default), the slot value is passed to the component as data via `content` prop (type CustomElementContent). This data gets rendered with the help of the `renderCustomElements()` utility, when no slot content is passed to the component. Thus, this way the slot renders correctly for both JSON and markup based rendering. Refer to [Render custom elements](/nuxt/render-custom-elements) docs for more details on that.
169170

170171
## First steps
171172

content/5.nuxt/30.render-custom-elements.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,37 @@ defineProps<{
3030
Rendering slots works differently depending on whether custom-elements are serialized into JSON or markup. In short, for rendering a slot in a Vue template, simply use the following snippet:
3131

3232
```vue
33-
<slot name="body">
34-
<component :is="useDrupalCe().renderCustomElements($attrs.body)" />
35-
</slot>
33+
<template>
34+
<slot name="body">
35+
<component :is="useDrupalCe().renderCustomElements(body)" />
36+
</slot>
37+
</template>
38+
39+
<script setup lang="ts">
40+
defineSlots<{
41+
body(): any;
42+
}>()
43+
defineProps<{
44+
body?: CustomElementContent;
45+
}>()
46+
</script>
3647
```
3748

38-
This snippet works with both, JSON and markup serialization.
49+
This snippet works with both, JSON and markup serialization. The markup serialization uses the slot, while the fallback value of the slot takes care of rendering the JSON-serialized value received as prop.
3950

4051
### JSON serialization (default)
4152

4253
Custom elements contained in slots get passed as JSON, which can be easily rendered with the helper provided by the nuxtjs-drupal-ce connector:
4354

4455
```vue
45-
<component :is="useDrupalCe().renderCustomElements($attrs.body)" />
56+
<template>
57+
<component :is="useDrupalCe().renderCustomElements(body)" />
58+
</template>
59+
60+
<script setup lang="ts">
61+
defineProps<{
62+
body?: CustomElementContent;
63+
}>()
4664
```
4765

4866
Note: When rendering via JSON content, the module supports [default components](/nuxt/default-components).
@@ -74,17 +92,18 @@ Here is an example of a component that leverages props and slots and works with
7492
<div class="node">
7593
<h2 v-if="title">Node: {{ title }}</h2>
7694
<slot name="body">
77-
<component :is="useDrupalCe().renderCustomElements($attrs.body)" />
95+
<component :is="useDrupalCe().renderCustomElements(body)" />
7896
</slot>
7997
</div>
8098
</template>
8199
82100
<script setup lang="ts">
83101
defineSlots<{
84-
body();
102+
body(): any;
85103
}>()
86104
defineProps<{
87105
title?: string;
106+
body?: CustomElementContent;
88107
}>()
89108
</script>
90109
```

0 commit comments

Comments
 (0)