Skip to content

Commit a1d0d0d

Browse files
author
GlennBecker
committed
Tests and Docs
1 parent a9c3756 commit a1d0d0d

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

packages/docs/src/pages/docs/components/anatomy.mdx

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Will result in:
100100

101101
Qwik host elements are marked with `q:host` attribute.
102102

103-
As you can see, `component$` will always create an extra element, by default is `div`, but that can be changed with `component$` options argument:
103+
As you can see, `component$` will always create an extra element, by default is `div`, but that can be changed with `component$` options argument or the `tagName` attribute:
104104

105105
```tsx
106106
const MyArticle = component$(() => (
@@ -110,7 +110,17 @@ const MyArticle = component$(() => (
110110
});
111111
```
112112

113-
Will result in:
113+
and:
114+
115+
```tsx
116+
const MyArticle = component$(() => (
117+
<span>My article</span>
118+
));
119+
120+
<MyArticle tagName="article"/>
121+
```
122+
123+
Will both result in:
114124

115125
```html
116126
<article q:host>
@@ -120,15 +130,14 @@ Will result in:
120130
</article>
121131
```
122132

133+
To control the tag name from inside of a `component$` use the [the Host tag](#host)
134+
123135
### `useHostElement()`
124136

125137
Since the host element is implicitly created by `component$`, it is not possible to access it directly. Instead, you can use `useHostElement()` to get the host element.
126138

127139
Qwik uses host elements in various ways. For example when using `useHostElement()` function which retrieves it. It is also used to attach props to the components for serialization.
128140

129-
130-
131-
132141
## Host Element Attributes & Styling
133142

134143
Assume you have a component defined as so:
@@ -230,6 +239,43 @@ will result in:
230239
</div>
231240
```
232241

242+
You can also use the `<Host/>` component to set the tag name of the wrapper element generate by Qwik. This is very useful when building design systems.
243+
244+
```tsx
245+
const Heading = component$((props: {as: 'h1' | 'h2' | 'h3'}) => {
246+
const styles = {
247+
h1: "font-size: 3rem",
248+
h2: "font-size: 2rem",
249+
h3: "font-size: 1.5rem",
250+
};
251+
return (
252+
<Host tagName={props.as} style={styles[props.as]}>
253+
<span>Hello World</span>
254+
</Host>
255+
);
256+
});
257+
```
258+
using the component like so:
259+
260+
```tsx
261+
<Heading as="h1" />
262+
<Heading as="h2" />
263+
<Heading as="h3" />
264+
```
265+
266+
will result in:
267+
268+
```html
269+
<h1 q:host>
270+
<span>Hello World</span>
271+
</h1>
272+
<h2 q:host>
273+
<span>Hello World</span>
274+
</h2>
275+
<h3 q:host>
276+
<span>Hello World</span>
277+
</h3>
278+
```
233279

234280
## Lazy Loading
235281

packages/qwik/src/core/render/render.unit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ describe('render', () => {
245245
});
246246

247247
it('should render host:tagName then a component', async () => {
248-
await render(fixture.host, <HelloWorld host:tagName={"article"}/>);
249-
expectFirstTag("article");
248+
await render(fixture.host, <HelloWorld host:tagName={'article'} />);
249+
expectFirstTag('article');
250250
});
251251

252252
describe('handlers', () => {

0 commit comments

Comments
 (0)