Skip to content

Commit 044e8b2

Browse files
committedJul 9, 2024··
Add example illustrating JSX literals, references
Related-to: GH-2503.
1 parent 90f509b commit 044e8b2

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed
 

‎docs/docs/using-mdx.mdx

+69-11
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,79 @@ The following keys can be passed in `components`:
309309
* HTML equivalents for the things you write with markdown such as `h1` for
310310
`# heading` (see [§ Table of components][toc] for examples)
311311
* `wrapper`, which defines the layout (but a local layout takes precedence)
312-
* anything else that is a valid JavaScript identifier (`foo`,
313-
`Quote`, `_`, `$x`, `a1`) for the things you write with JSX (like
312+
* anything else that is a valid JSX identifier (`foo`, `Quote`,
313+
`custom-element`, `_`, `$x`, `a1`) for the things you write with JSX (like
314314
`<So />` or `<like.so />`, note that locally defined components take
315315
precedence)**‡**
316316
317-
**‡** If you ever wondered what the rules are for whether a name in JSX (so `x`
318-
in `<x>`) is a literal tag name (like `h1`) or not (like `Component`), they are
319-
as follows:
317+
**‡** The rules for whether a name in JSX (so `x` in `<x>`) is a literal tag
318+
name (like `h1`) or not (like `Component`) are as follows:
319+
320+
* if there’s a dot,
321+
it’s a member expression (`<a.b>``h(a.b)`),
322+
which means a reference to the key `b` taken from object `a`
323+
* otherwise,
324+
if the name is not a valid JS identifier,
325+
it’s a literal (`<a-b>``h('a-b')`)
326+
* otherwise,
327+
if it starts with a lowercase,
328+
it’s a literal (`<a>``h('a')`)
329+
* otherwise,
330+
it’s a reference (`<A>``h(A)`)
331+
332+
These keys in `components` and the difference between literal tag names and
333+
references is illustrated as follows.
334+
With the following MDX:
320335
321-
* If there’s a dot, it’s a member expression (`<a.b>` -> `h(a.b)`),
322-
which means that the `b` component is taken from object `a`
323-
* Otherwise, if the name is not a valid identifier, it’s a literal (`<a-b>` ->
324-
`h('a-b')`)
325-
* Otherwise, if it starts with a lowercase, it’s a literal (`<a>` -> `h('a')`)
326-
* Otherwise, it’s an identifier (`<A>` -> `h(A)`), which means a component `A`
336+
```mdx path="example.mdx"
337+
* [markdown syntax](#alpha)
338+
* <a href="#bravo">JSX with a lowercase name</a>
339+
* <Link to="#charlie">JSX with a capitalized name</Link>
340+
```
341+
342+
…passed some components:
343+
344+
```jsx twoslash path="example.jsx"
345+
// @filename: types.d.ts
346+
import type {} from 'mdx'
347+
// @filename: example.jsx
348+
/// <reference lib="dom" />
349+
/* @jsxImportSource react */
350+
// ---cut---
351+
import Example from './example.mdx'
352+
353+
console.log(
354+
<Example
355+
components={{
356+
a(props) {
357+
return <a {...props} style={{borderTop: '1px dotted', color: 'violet'}} />
358+
},
359+
Link(props) {
360+
return <a href={props.to} children={props.children} style={{borderTop: '1px dashed', color: 'tomato'}} />
361+
}
362+
}}
363+
/>
364+
)
365+
```
366+
367+
…we’d get:
368+
369+
{
370+
<div className="card">
371+
<ul>
372+
<li><a href="#alpha" style={{borderTop: '1px dotted', color: 'violet'}}>markdown syntax</a></li>
373+
<li><a href="#bravo">JSX with a lowercase name</a></li>
374+
<li><a href="#charlie" style={{borderTop: '1px dashed', color: 'tomato'}}>JSX with a capitalized name</a></li>
375+
</ul>
376+
</div>
377+
}
378+
379+
Observe that the first link (`#alpha`) is dotted and violet.
380+
That’s because `a` is the HTML equivalent for the markdown syntax being used.
381+
The second link (`#bravo`) remains unchanged,
382+
because in JSX syntax `a` is a literal tag name.
383+
The third link (`#charlie`) is dashed and tomato,
384+
as in JSX syntax `Link` is a reference.
327385
328386
### Layout
329387

0 commit comments

Comments
 (0)
Please sign in to comment.