Skip to content

Commit

Permalink
feat: extend functionality of usePortableText
Browse files Browse the repository at this point in the history
  • Loading branch information
theisel committed Oct 28, 2022
1 parent 36a2296 commit 9ba2709
Show file tree
Hide file tree
Showing 21 changed files with 329 additions and 205 deletions.
22 changes: 22 additions & 0 deletions .changeset/giant-mugs-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"astro-portabletext": minor
---

Feat: `usePortableText(node)` utility function returns new properties and some have been deprecated.

Refer to the `utils docs` for usage.

**Added**

| Property | Type | Description |
| :------------------------------ | :--------- | :------------------------------------------------------------------------------------------------------------------------------- |
| getDefaultComponent | `function` | Returns the `default` component related to the passed in `node`. Use this when you need to fall back to the `default` component. |
| getUnknownComponent | `function` | Returns the counterpart `unknown(Block\|List\|ListItem\|Mark\|Type)` component related to the passed in `node`. |
| notifyMissingComponentHandler | `function` | Calls `onMissingComponent` handler. Pass in a `string` to override `default` message. |

**Deprecated**

| Property | Type | Description |
| :------------------------------ | :--------- | :------------------------------------------------------------------------------------------------------------------------------- |
| next | `function` | Use `getDefaultComponent` |
| onMissingComponent | `function` | Use `notifyMissingComponentHandler` |
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ pnpm-lock.yaml
# Components
astro-portabletext/components/Block.astro
astro-portabletext/components/Mark.astro
astro-portabletext/components/Raw.astro
8 changes: 5 additions & 3 deletions astro-portabletext/components/Block.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export type Props = $<Block>;
const props = Astro.props;
const { node, index, isInline, ...attrs } = props;
const { next } = usePortableText(node);
const styleIs = (style: string) => style === node.style;
const Next = next();
const { getUnknownComponent } = usePortableText(node);
const UnknownStyle = getUnknownComponent();
---

{
Expand All @@ -29,6 +31,6 @@ const Next = next();
) : styleIs("normal") ? (
<p {...attrs}><slot /></p>
) : (
<Next {...props}><slot /></Next>
<UnknownStyle {...props}><slot /></UnknownStyle>
)
}
8 changes: 0 additions & 8 deletions astro-portabletext/components/Empty.astro

This file was deleted.

8 changes: 5 additions & 3 deletions astro-portabletext/components/Mark.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export type Props = $<Mark>;
const props = Astro.props;
const { node, index, isInline, ...attrs } = props;
const { next } = usePortableText(node);
const markTypeIs = (markType: string) => markType === node.markType;
const Next = next();
const { getUnknownComponent } = usePortableText(node);
const UnknownMarkType = getUnknownComponent();
---

{
Expand All @@ -25,6 +27,6 @@ const Next = next();
) : markTypeIs("underline") ? (
<span style="text-decoration: underline;" {...attrs}><slot /></span>
) : (
<Next {...props}><slot /></Next>
<UnknownMarkType {...props}><slot /></UnknownMarkType>
)
}
84 changes: 0 additions & 84 deletions astro-portabletext/components/NodeRenderer.astro

This file was deleted.

Loading

0 comments on commit 9ba2709

Please sign in to comment.