Skip to content

Commit

Permalink
update portable-text ui element
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Sep 21, 2024
1 parent a360a36 commit 42fe354
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/astro/src/components/ui/portable-text/Block.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot />
21 changes: 21 additions & 0 deletions apps/astro/src/components/ui/portable-text/Mark.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
import type { Props as $, Mark as MarkType } from 'astro-portabletext/types'
import { Mark as PortableTextMark } from 'astro-portabletext/components'
export type Props = $<MarkType<never>>
const { node, ...props } = Astro.props
const { href, type } = node.markDef as { href: string; type: 'external' | 'internal' }
const isExternal = type === 'external'
---

{
node.markType === 'link' ? (
<a href={href} {...(isExternal && { target: '_blank', rel: 'noreferrer' })} {...props}>
<slot />
</a>
) : (
PortableTextMark
)
}
36 changes: 36 additions & 0 deletions apps/astro/src/components/ui/portable-text/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
import type { HTMLAttributes } from 'astro/types'
import { PortableText } from 'astro-portabletext'
import type { PortableTextProps } from 'astro-portabletext/types'
import Mark from './Mark.astro'
import Block from './Block.astro'
export type PortableTextValue = PortableTextProps['value']
type Props = {
value: PortableTextValue
heading?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
} & HTMLAttributes<'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'>
const { heading: HeadingTag, value, ...props } = Astro.props
const valueArray = Array.isArray(value) ? value : [value]
---

{
HeadingTag ? (
<HeadingTag {...props}>
{valueArray.map((value: PortableTextValue, index: number) =>
index > 0 ? (
<span {...props}>{<PortableText value={value} components={{ block: Block, mark: Mark }} />}</span>
) : (
<PortableText value={value} components={{ block: Block, mark: Mark }} />
)
)}
</HeadingTag>
) : (
<div {...props}>
<PortableText value={value} components={{ mark: Mark }} />
</div>
)
}
15 changes: 15 additions & 0 deletions apps/astro/src/components/ui/portable-text/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export { default, type PortableTextValue } from './index.astro';

export const PortableTextQuery = (name: string) => `
${name}[] {
...,
markDefs[] {
_type == "link" => {
_type,
_key,
type,
"href": select(type == "internal" => internal -> slug.current, type == "external" => external, "#"),
},
},
},
`

0 comments on commit 42fe354

Please sign in to comment.