Skip to content

Commit 8e9268a

Browse files
Wired up button toggle to CTA Card (#1415)
ref https://linear.app/ghost/issue/PLG-318/implement-button-toggle - Wired up a function to toggle the visibility of the button on the CTA card. - Ensures the Button settings expand and collapse when toggling it. - Added tests
1 parent 31ad767 commit 8e9268a

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

packages/koenig-lexical/src/components/ui/cards/CtaCard.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ export function CtaCard({
7373
isSelected, //
7474
layout, //
7575
showButton, //
76-
updateButtonText, //
77-
updateButtonUrl, //
78-
updateShowButton, //
79-
updateHasSponsorLabel, //
80-
updateLayout, //
81-
handleColorChange, //
82-
handleButtonColor //
76+
updateButtonText,
77+
updateButtonUrl,
78+
updateShowButton,
79+
updateHasSponsorLabel,
80+
updateLayout,
81+
handleColorChange,
82+
handleButtonColor
8383
}) {
8484
const [buttonColorPickerExpanded, setButtonColorPickerExpanded] = useState(false);
8585

packages/koenig-lexical/src/nodes/CallToActionNodeComponent.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import CardContext from '../context/CardContext';
22
import KoenigComposerContext from '../context/KoenigComposerContext.jsx';
33
import React from 'react';
4+
import {$getNodeByKey} from 'lexical';
45
import {ActionToolbar} from '../components/ui/ActionToolbar.jsx';
56
import {CtaCard} from '../components/ui/cards/CtaCard';
67
import {SnippetActionToolbar} from '../components/ui/SnippetActionToolbar.jsx';
78
import {ToolbarMenu, ToolbarMenuItem, ToolbarMenuSeparator} from '../components/ui/ToolbarMenu.jsx';
9+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
810

911
export const CallToActionNodeComponent = ({
1012
nodeKey,
@@ -21,7 +23,7 @@ export const CallToActionNodeComponent = ({
2123
buttonColor,
2224
htmlEditor
2325
}) => {
24-
// const [editor] = useLexicalComposerContext();
26+
const [editor] = useLexicalComposerContext();
2527
const {isEditing, isSelected, setEditing} = React.useContext(CardContext);
2628
const {cardConfig} = React.useContext(KoenigComposerContext);
2729
const [showSnippetToolbar, setShowSnippetToolbar] = React.useState(false);
@@ -30,6 +32,14 @@ export const CallToActionNodeComponent = ({
3032
event.stopPropagation();
3133
setEditing(true);
3234
};
35+
36+
const toggleShowButton = (event) => {
37+
editor.update(() => {
38+
const node = $getNodeByKey(nodeKey);
39+
node.showButton = !node.showButton;
40+
});
41+
};
42+
3343
return (
3444
<>
3545
<CtaCard
@@ -54,7 +64,7 @@ export const CallToActionNodeComponent = ({
5464
updateButtonUrl={() => {}}
5565
updateHasSponsorLabel={() => {}}
5666
updateLayout={() => {}}
57-
updateShowButton={() => {}}
67+
updateShowButton={toggleShowButton}
5868
/>
5969
<ActionToolbar
6070
data-kg-card-toolbar="button"

packages/koenig-lexical/test/e2e/cards/cta-card.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assertHTML, focusEditor, html, initialize, insertCard} from '../../utils/e2e';
2-
import {test} from '@playwright/test';
2+
import {expect, test} from '@playwright/test';
33

44
test.describe('Call To Action Card', async () => {
55
let page;
@@ -28,4 +28,31 @@ test.describe('Call To Action Card', async () => {
2828
<p><br /></p>
2929
`, {ignoreCardContents: true});
3030
});
31+
32+
test('can toggle button on card', async function () {
33+
await focusEditor(page);
34+
await insertCard(page, {cardName: 'call-to-action'});
35+
await page.click('[data-testid="button-settings"]');
36+
expect(await page.isVisible('[data-testid="cta-button"]')).toBe(true);
37+
38+
await page.click('[data-testid="button-settings"]');
39+
40+
expect(await page.isVisible('[data-testid="cta-button"]')).toBe(false);
41+
});
42+
43+
test('button settings expands and collapses when toggled', async function () {
44+
await focusEditor(page);
45+
await insertCard(page, {cardName: 'call-to-action'});
46+
await page.click('[data-testid="button-settings"]');
47+
// determine if settings are open byy looking for cta-button-color, button-text & button-url
48+
expect(await page.isVisible('[data-testid="cta-button-color"]')).toBe(true);
49+
expect(await page.isVisible('[data-testid="button-text"]')).toBe(true);
50+
expect(await page.isVisible('[data-testid="button-url"]')).toBe(true);
51+
52+
await page.click('[data-testid="button-settings"]');
53+
// determine if settings are closed by looking for cta-button-color, button-text & button-url
54+
expect(await page.isVisible('[data-testid="cta-button-color"]')).toBe(false);
55+
expect(await page.isVisible('[data-testid="button-text"]')).toBe(false);
56+
expect(await page.isVisible('[data-testid="button-url"]')).toBe(false);
57+
});
3158
});

0 commit comments

Comments
 (0)