|
2 | 2 | title: Render |
3 | 3 | --- |
4 | 4 |
|
5 | | -import { Steps } from "~/components"; |
| 5 | +import { Code, Details, Type, MetaInfo } from "~/components"; |
6 | 6 |
|
7 | 7 | The `Render` component allows us to include a "partial", a reusable Markdown snippet, onto a page. |
8 | 8 |
|
9 | | -It also accepts parameters that can be used as variables within the partial, so that even content |
10 | | -which needs slight differences between usages can be turned into a partial. |
| 9 | +It also accepts parameters that can be used as variables within the partial, so that even content which needs slight differences between usages can be turned into a partial. |
11 | 10 |
|
12 | 11 | ## Component |
13 | 12 |
|
14 | 13 | ```mdx live |
15 | 14 | import { Render } from "~/components"; |
16 | 15 |
|
17 | | -<Render file="hello" params={{ |
| 16 | +<Render file="simple-props" params={{ |
18 | 17 | name: "world", |
19 | | - link: "/style-guide/components/render/" |
20 | 18 | }} /> |
21 | | -{/* |
22 | | -Hello, {props.name}! |
| 19 | +``` |
23 | 20 |
|
24 | | -Hello `{props.name}` |
| 21 | +### Inputs |
25 | 22 |
|
26 | | -Hello <code>{props.name}</code> |
| 23 | +- `file` <Type text="string" /> |
27 | 24 |
|
28 | | -[link]({props.link}) |
| 25 | + This should be the name of the partial, without the containing directory or file extension. For example, `/partials/style-guide/hello.mdx` would be `file="hello"`. |
29 | 26 |
|
30 | | -<a href={props.link}>link</a> |
31 | | -*/} |
32 | | -``` |
| 27 | +- `product` <Type text="string" /> <MetaInfo text="optional" /> |
33 | 28 |
|
34 | | -### Inputs |
| 29 | + By default, it will look for partials in the same product folder as the current page. You can use this to specify a different product. |
| 30 | + |
| 31 | + :::caution |
| 32 | + |
| 33 | + When using the `Render` component inside partials, the original `product` is lost. |
| 34 | + |
| 35 | + For example, if there are three files: |
| 36 | + |
| 37 | + 1. `docs/fundamentals/index.mdx` |
| 38 | + 2. `partials/dns/thing.mdx` |
| 39 | + 3. `partials/dns/thing2.mdx` |
35 | 40 |
|
36 | | -**file** `string`: This should be the name of the partial, without the containing directory or file extension. |
37 | | -For example, `/partials/style-guide/hello.mdx` would be `file="hello"`. |
| 41 | + `docs/fundamentals/index.mdx` uses `<Render file="thing" product="dns" />` |
38 | 42 |
|
39 | | -**product** `string` (optional): By default, it will look for partials in the same product folder as the current page. |
40 | | -You can use this to specify a different product. |
| 43 | + `partials/dns/thing.mdx` must use `<Render file="thing2" product="dns" />` as `product` cannot be inferred. |
41 | 44 |
|
42 | | -**params** `object` (optional): If you wish to substitute values inside your partial, you can use pass params which can be |
43 | | -referenced in your partial. Refer to [params](#params). |
| 45 | + ::: |
44 | 46 |
|
45 | | -## Partials |
| 47 | +- `params` <Type text="object" /> <MetaInfo text="optional" /> |
46 | 48 |
|
47 | | -Partials only have one optional frontmatter property, which is `params`. This takes an array of strings, |
48 | | -which should be the expected parameters. When this is defined, but those parameters are not passed, an error |
49 | | -will be thrown. |
| 49 | + If you wish to substitute values inside your partial, you can use pass params which can be referenced in your partial. Refer to [properties](#properties). |
50 | 50 |
|
51 | | -```mdx title="/src/content/partials/style-guide/hello.mdx" |
| 51 | +## Properties |
| 52 | + |
| 53 | +### Defining expected properties in frontmatter |
| 54 | + |
| 55 | +Anything defined in the `params` property of the `Render` component is available inside the partial, using [JavaScript expressions](https://mdxjs.com/docs/using-mdx/). |
| 56 | + |
| 57 | +To protect against required properties being missed, any partial that relies on `params` should also define `params` in the partial's frontmatter. This should be an array of strings, matching the property names you expect. If a property is optional, such as for [conditional content](#properties-to-render-content-conditionally), add a `?` to the end of the name. |
| 58 | + |
| 59 | +```mdx |
52 | 60 | --- |
53 | 61 | params: |
54 | | - - name |
55 | | - - foo |
56 | | - - bar |
| 62 | + - product |
| 63 | + - deprecated? |
57 | 64 | --- |
| 65 | +``` |
| 66 | + |
| 67 | +For each of the below examples, you can open the dropdown to view the partial's content. |
| 68 | + |
| 69 | +### Properties as a plain string |
| 70 | + |
| 71 | +The below example would render `Hello, world!`. |
| 72 | + |
| 73 | +import simpleRaw from "~/content/partials/style-guide/simple-props.mdx?raw"; |
| 74 | + |
| 75 | +<Details header="simple-props.mdx"> |
| 76 | + <Code code={simpleRaw} lang="mdx" /> |
| 77 | +</Details> |
| 78 | + |
| 79 | +```mdx live |
| 80 | +import { Render } from "~/components"; |
| 81 | + |
| 82 | +<Render file="simple-props" params={{ name: "world" }} /> |
| 83 | +``` |
| 84 | + |
| 85 | +### Properties in Markdown syntax |
| 86 | + |
| 87 | +When using JavaScript expressions, you are now "inside JSX" and cannot use traditional Markdown syntax. Similarly, you cannot use a JavaScript expression inside Markdown syntax. |
| 88 | + |
| 89 | +Ideally, you should not use Markdown syntax, such as `**strong**` or `[text](link)`, with properties. If using JSX is not feasible, there is a [`Markdown`](/style-guide/components/markdown/) component that will take a `text` property. |
| 90 | + |
| 91 | +The [MDX documentation](https://mdxjs.com/table-of-components/#components) includes a mapping of common Markdown syntax to their equivalent JSX elements. |
| 92 | + |
| 93 | +#### Strong |
| 94 | + |
| 95 | +import strongRaw from "~/content/partials/style-guide/strong-in-props.mdx?raw"; |
58 | 96 |
|
59 | | -Hello, {props.name}! |
| 97 | +<Details header="strong-in-props.mdx"> |
| 98 | + <Code code={strongRaw} lang="mdx" /> |
| 99 | +</Details> |
60 | 100 |
|
61 | | -Hello, {props.foo}! |
| 101 | +```mdx live |
| 102 | +import { Render } from "~/components"; |
62 | 103 |
|
63 | | -Hello, {props.bar}! |
| 104 | +<Render file="strong-in-props" params={{ do: "Text", dont: "**Text**" }} /> |
64 | 105 | ``` |
65 | 106 |
|
66 | | -### Params |
| 107 | +#### Links |
| 108 | + |
| 109 | +import linkRaw from "~/content/partials/style-guide/link-in-props.mdx?raw"; |
| 110 | + |
| 111 | +<Details header="link-in-props.mdx"> |
| 112 | + <Code code={linkRaw} lang="mdx" /> |
| 113 | +</Details> |
67 | 114 |
|
68 | | -In the above example, you will notice there is: |
| 115 | +```mdx live |
| 116 | +import { Render } from "~/components"; |
69 | 117 |
|
70 | | -<Steps> |
| 118 | +<Render file="link-in-props" params={{ |
| 119 | + link: "/style-guide/components/render/#links" |
| 120 | +}} /> |
| 121 | +``` |
71 | 122 |
|
72 | | -1. A `params` input on the `Render` component. |
73 | | -2. A `params` property in the frontmatter. |
74 | | -3. A reference to `props.name`. |
| 123 | +#### Images |
| 124 | + |
| 125 | +import imageRaw from "~/content/partials/style-guide/image-in-props.mdx?raw"; |
| 126 | + |
| 127 | +<Details header="image-in-props.mdx"> |
| 128 | + <Code code={imageRaw} lang="mdx" /> |
| 129 | +</Details> |
| 130 | + |
| 131 | +```mdx live |
| 132 | +import { Render } from "~/components"; |
| 133 | + |
| 134 | +<Render file="image-in-props" params={{ image: "/logo.svg" }} /> |
| 135 | +``` |
75 | 136 |
|
76 | | -</Steps> |
| 137 | +#### Code blocks |
77 | 138 |
|
78 | | -#### Input |
| 139 | +import codeRaw from "~/content/partials/style-guide/code-in-props.mdx?raw"; |
| 140 | + |
| 141 | +<Details header="code-in-props.mdx"> |
| 142 | + <Code code={codeRaw} lang="mdx" /> |
| 143 | +</Details> |
| 144 | + |
| 145 | +```mdx live |
| 146 | +import { Render } from "~/components"; |
| 147 | + |
| 148 | +<Render file="code-in-props" params={{ code: "export const foo = 'bar';" }} /> |
| 149 | +``` |
| 150 | + |
| 151 | +### Properties to render content conditionally |
| 152 | + |
| 153 | +Anything that you can represent in a JavaScript expression can be used in your conditional logic. |
| 154 | + |
| 155 | +This may be the [and (`&&`) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND) or [ternary (`? ... : ... `) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator), the below example uses both. |
| 156 | + |
| 157 | +import optionalRaw from "~/content/partials/style-guide/optional-props.mdx?raw"; |
| 158 | + |
| 159 | +<Details header="optional-props.mdx"> |
| 160 | + <Code code={optionalRaw} lang="mdx" /> |
| 161 | +</Details> |
| 162 | + |
| 163 | +```mdx live |
| 164 | +import { Render } from "~/components"; |
79 | 165 |
|
80 | | -When using the `params` input on the `Render` component, you can write a [JavaScript object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects) |
81 | | -which is later available inside the partial. |
| 166 | +<Render file="optional-props" params={{ product: "Thing", deprecated: true }} /> |
82 | 167 |
|
83 | | -#### Frontmatter |
| 168 | +<hr /> |
84 | 169 |
|
85 | | -The `params` frontmatter property on a partial expects an array of strings, containing the "expected parameters". This is so that if |
86 | | -your partial requires parameters to be passed, and none are passed, we can warn the user. |
| 170 | +<Render file="optional-props" params={{ product: "Thing Two" }} /> |
87 | 171 |
|
88 | | -#### Props |
| 172 | +<hr /> |
89 | 173 |
|
90 | | -The way that you can access these parameters is with the `props` object, surrounded in curly braces `{}`. |
91 | | -This is a [JavaScript expression within MDX](https://mdxjs.com/docs/using-mdx/#props). |
| 174 | +<Render file="optional-props" params={{ product: "Thing Three" }} /> |
| 175 | +``` |
0 commit comments