|
2 | 2 | title: Render |
3 | 3 | --- |
4 | 4 |
|
5 | | -import { Code, Details, Type, MetaInfo } from "~/components"; |
| 5 | +import { Steps } 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 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 |
| 10 | +which needs slight differences between usages can be turned into a partial. |
10 | 11 |
|
11 | 12 | ## Component |
12 | 13 |
|
13 | 14 | ```mdx live |
14 | 15 | import { Render } from "~/components"; |
15 | 16 |
|
16 | | -<Render file="simple-props" params={{ |
| 17 | +<Render file="hello" params={{ |
17 | 18 | name: "world", |
| 19 | + link: "/style-guide/components/render/" |
18 | 20 | }} /> |
19 | | -``` |
20 | | - |
21 | | -### Inputs |
22 | | - |
23 | | -- `file` <Type text="string" /> |
24 | | - |
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"`. |
26 | | - |
27 | | -- `product` <Type text="string" /> <MetaInfo text="optional" /> |
28 | | - |
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 |
| 21 | +{/* |
| 22 | +Hello, {props.name}! |
32 | 23 |
|
33 | | - When using the `Render` component inside partials, the original `product` is lost. |
| 24 | +Hello `{props.name}` |
34 | 25 |
|
35 | | - For example, if there are three files: |
| 26 | +Hello <code>{props.name}</code> |
36 | 27 |
|
37 | | - 1. `docs/fundamentals/index.mdx` |
38 | | - 2. `partials/dns/thing.mdx` |
39 | | - 3. `partials/dns/thing2.mdx` |
| 28 | +[link]({props.link}) |
40 | 29 |
|
41 | | - `docs/fundamentals/index.mdx` uses `<Render file="thing" product="dns" />` |
42 | | - |
43 | | - `partials/dns/thing.mdx` must use `<Render file="thing2" product="dns" />` as `product` cannot be inferred. |
44 | | - |
45 | | - ::: |
| 30 | +<a href={props.link}>link</a> |
| 31 | +*/} |
| 32 | +``` |
46 | 33 |
|
47 | | -- `params` <Type text="object" /> <MetaInfo text="optional" /> |
| 34 | +### Inputs |
48 | 35 |
|
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). |
| 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"`. |
50 | 38 |
|
51 | | -## Properties |
| 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. |
52 | 41 |
|
53 | | -### Defining expected properties in frontmatter |
| 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). |
54 | 44 |
|
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/). |
| 45 | +## Partials |
56 | 46 |
|
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. |
| 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. |
58 | 50 |
|
59 | | -```mdx |
| 51 | +```mdx title="/src/content/partials/style-guide/hello.mdx" |
60 | 52 | --- |
61 | 53 | params: |
62 | | - - product |
63 | | - - deprecated? |
| 54 | + - name |
| 55 | + - foo |
| 56 | + - bar |
64 | 57 | --- |
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"; |
96 | 58 |
|
97 | | -<Details header="strong-in-props.mdx"> |
98 | | - <Code code={strongRaw} lang="mdx" /> |
99 | | -</Details> |
| 59 | +Hello, {props.name}! |
100 | 60 |
|
101 | | -```mdx live |
102 | | -import { Render } from "~/components"; |
| 61 | +Hello, {props.foo}! |
103 | 62 |
|
104 | | -<Render file="strong-in-props" params={{ do: "Text", dont: "**Text**" }} /> |
| 63 | +Hello, {props.bar}! |
105 | 64 | ``` |
106 | 65 |
|
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> |
| 66 | +### Params |
114 | 67 |
|
115 | | -```mdx live |
116 | | -import { Render } from "~/components"; |
| 68 | +In the above example, you will notice there is: |
117 | 69 |
|
118 | | -<Render file="link-in-props" params={{ |
119 | | - link: "/style-guide/components/render/#links" |
120 | | -}} /> |
121 | | -``` |
| 70 | +<Steps> |
122 | 71 |
|
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 | | -``` |
| 72 | +1. A `params` input on the `Render` component. |
| 73 | +2. A `params` property in the frontmatter. |
| 74 | +3. A reference to `props.name`. |
136 | 75 |
|
137 | | -#### Code blocks |
| 76 | +</Steps> |
138 | 77 |
|
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"; |
| 78 | +#### Input |
165 | 79 |
|
166 | | -<Render file="optional-props" params={{ product: "Thing", deprecated: true }} /> |
| 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. |
167 | 82 |
|
168 | | -<hr /> |
| 83 | +#### Frontmatter |
169 | 84 |
|
170 | | -<Render file="optional-props" params={{ product: "Thing Two" }} /> |
| 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. |
171 | 87 |
|
172 | | -<hr /> |
| 88 | +#### Props |
173 | 89 |
|
174 | | -<Render file="optional-props" params={{ product: "Thing Three" }} /> |
175 | | -``` |
| 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). |
0 commit comments