-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introducing rsc-compatible image block (#835)
- Loading branch information
1 parent
b09b696
commit 915c6b6
Showing
12 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@headstartwp/core": minor | ||
"@headstartwp/next": minor | ||
--- | ||
|
||
RSC compatible image block for next.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { domToReact, Element, isBlockByName } from '@headstartwp/core'; | ||
import { BlockFC, BlockProps } from '@headstartwp/core/react'; | ||
import { ImageComponent } from '../../components/ImageComponent'; | ||
|
||
type ImageBlockAttributes = { | ||
width?: number; | ||
height?: number; | ||
sizeSlug?: string; | ||
linkDestination?: string; | ||
src: string; | ||
alt?: string; | ||
}; | ||
|
||
export const ImageBlock: BlockFC<BlockProps<ImageBlockAttributes>> = ({ | ||
domNode: node, | ||
children, | ||
style, | ||
block, | ||
}) => { | ||
if (typeof node === 'undefined') { | ||
return null; | ||
} | ||
|
||
if (typeof block === 'undefined') { | ||
return domToReact([node]); | ||
} | ||
|
||
const { name, className, attributes } = block; | ||
|
||
const hasFigureNode = (node.firstChild as Element).name === 'figure'; | ||
|
||
const { width, height } = attributes; | ||
let imgNode; | ||
|
||
if (hasFigureNode) { | ||
const figureNode = node.children[0] as Element; | ||
|
||
if (figureNode.children[0]) { | ||
imgNode = figureNode.children[0] as Element; | ||
} | ||
} else { | ||
imgNode = node.children[0] as Element; | ||
} | ||
|
||
const { src, alt, width: imgNodeWidth, height: imgNodeHeight } = imgNode.attribs; | ||
const imageWidth = width ?? imgNodeWidth; | ||
const imageHeight = height ?? imgNodeHeight; | ||
|
||
return ( | ||
<ImageComponent | ||
name={name} | ||
domNode={node} | ||
className={className} | ||
src={src} | ||
alt={alt} | ||
width={imageWidth ? Number(imageWidth) : undefined} | ||
height={imageHeight ? Number(imageHeight) : undefined} | ||
style={style} | ||
> | ||
{children} | ||
</ImageComponent> | ||
); | ||
}; | ||
|
||
ImageBlock.test = (node) => { | ||
return isBlockByName(node, 'core/image'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters