Skip to content

Commit

Permalink
Feature to add anchor attribute to core/paragraph block (#2015)
Browse files Browse the repository at this point in the history
* [feat] Add anchor attribute to core/paragraph block

* Added test for Core/Paragraph block

* chore: changeset message update, package version boost v6.0.0
  • Loading branch information
jan-clockworkwp authored Jan 14, 2025
1 parent bdb7d7f commit 99b5793
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .changeset/popular-glasses-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@faustwp/blocks': major
---

Update of the CoreParagraph block to support the native WP anchor attribute. GitHub issue: "[[feat] Add anchor attribute to core/paragraph block](https://github.com/wpengine/faustjs/issues/1954)"

Introduces new field to `core/paragraph` block: `anchor`. This field allows users to add an anchor to the paragraph block. The anchor is used to create a link to a specific part of the page. The anchor is added to the block's wrapper element as an ID attribute.

**Files changed:**
- packages/blocks/src/blocks/CoreParagraph.tsx (added anchor attribute)
- packages/blocks/package.json (updated package version to 6.0.0)
2 changes: 1 addition & 1 deletion packages/blocks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@faustwp/blocks",
"version": "5.0.0",
"version": "6.0.0",
"description": "Faust Blocks",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/blocks/src/blocks/CoreParagraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type CoreParagraphFragmentProps = ContentBlock & {
dropCap?: string;
gradient?: string;
align?: string;
anchor?: string;
};
};

Expand All @@ -30,6 +31,7 @@ export function CoreParagraph(props: CoreParagraphFragmentProps) {
<p
style={style}
className={attributes?.cssClassName}
id={attributes?.anchor}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: attributes?.content ?? '' }}
/>
Expand All @@ -52,6 +54,7 @@ CoreParagraph.fragments = {
dropCap
gradient
align
anchor
}
}
`,
Expand Down
16 changes: 16 additions & 0 deletions packages/blocks/tests/blocks/CoreParagraph.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ describe('<CoreParagraph />', () => {
</p>
`);
});

test('applies the HTML anchor attribute properly', () => {
renderProvider({
attributes: {
anchor: 'test-anchor',
content: 'Hello World',
},
});
expect(screen.queryByText('Hello World')).toMatchInlineSnapshot(`
<p
id="test-anchor"
>
Hello World
</p>
`);
});
});

0 comments on commit 99b5793

Please sign in to comment.