Skip to content

Commit

Permalink
feat: interating on rendering blocks on Gutenberg
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Oct 31, 2024
1 parent a3e72f2 commit 5752792
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
29 changes: 23 additions & 6 deletions packages/block-primitives/src/block-editor/block.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactNode, createContext, useContext, useMemo } from 'react';
import React, { ReactNode, createContext, isValidElement, useContext, useMemo } from 'react';
import { UniversalBlock } from '#shared/types.js';

const BlockPrimitiveContext = createContext<BlockProps>({
const BlockPrimitiveContext = createContext<UniversalBlockProviderProps>({
attributes: {},
setAttributes: () => {
throw new Error(
Expand All @@ -9,7 +10,7 @@ const BlockPrimitiveContext = createContext<BlockProps>({
},
});

type BlockProps = {
type UniversalBlockProviderProps = {
attributes: Record<string, any>;
setAttributes: (attributes: Record<string, any>) => void;
};
Expand All @@ -20,12 +21,28 @@ export function useBlockPrimitiveProps() {
return props;
}

const Block = ({ attributes, setAttributes, children }: BlockProps & { children: ReactNode }) => {
const UniversalBlockProvider = ({
attributes,
setAttributes,
children,
}: UniversalBlockProviderProps & { children: ReactNode }) => {
const value = useMemo(() => ({ setAttributes, attributes }), [attributes, setAttributes]);
const blocks: ReactNode[] = React.Children.toArray(children);

return (
<BlockPrimitiveContext.Provider value={value}>{children}</BlockPrimitiveContext.Provider>
<BlockPrimitiveContext.Provider value={value}>
{blocks.map((block) => {
if (isValidElement<UniversalBlock>(block)) {
return React.cloneElement(block, {
...block.props,
attributes,
});
}

return block;
})}
</BlockPrimitiveContext.Provider>
);
};

export default Block;
export default UniversalBlockProvider;
2 changes: 1 addition & 1 deletion packages/block-primitives/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface InnerBlocksProps extends InnerBlocks.Props {
className?: string;
}

export interface UniversalBlock<T extends Record<string, unknown>> {
export interface UniversalBlock<T extends Record<string, unknown> = Record<string, unknown>> {
children?: React.ReactNode;
attributes: {
[k in keyof T]: T[k];
Expand Down
8 changes: 4 additions & 4 deletions wp/10up-theme/includes/blocks/example-block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { useBlockProps } from '@wordpress/block-editor';

import Block from '@headstartwp/block-primitives/block';
import UniversalBlockProvider from '@headstartwp/block-primitives/block';
import { Hero } from '@headstartwp/component-library/hero';

/**
Expand All @@ -24,9 +24,9 @@ const ExampleBlockEdit = (props) => {

return (
<div {...blockProps}>
<Block attributes={attributes} setAttributes={setAttributes}>
<Hero attributes={attributes} />
</Block>
<UniversalBlockProvider attributes={attributes} setAttributes={setAttributes}>
<Hero />
</UniversalBlockProvider>
</div>
);
};
Expand Down

0 comments on commit 5752792

Please sign in to comment.