Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added layout toggle to CTA Card #1422

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {generateDecoratorNode} from '../../generate-decorator-node';

export class CallToActionNode extends generateDecoratorNode({nodeType: 'call-to-action',
properties: [
{name: 'layout', default: 'immersive'},
{name: 'layout', default: 'minimal'},
{name: 'textValue', default: '', wordCount: true},
{name: 'showButton', default: false},
{name: 'buttonText', default: ''},
Expand Down Expand Up @@ -33,7 +33,7 @@ export class CallToActionNode extends generateDecoratorNode({nodeType: 'call-to-
imageUrl
} = {}, key) {
super(key);
this.__layout = layout || 'immersive';
this.__layout = layout || 'minimal';
this.__textValue = textValue || '';
this.__showButton = showButton || false;
this.__buttonText = buttonText || '';
Expand Down
4 changes: 2 additions & 2 deletions packages/kg-default-nodes/test/nodes/call-to-action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('CallToActionNode', function () {
beforeEach(function () {
editor = createHeadlessEditor({nodes: editorNodes});
dataset = {
layout: 'immersive',
layout: 'minimal',
textValue: 'This is a cool advertisement',
showButton: true,
buttonText: 'click me',
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('CallToActionNode', function () {
it('has setters for all properties', editorTest(function () {
const callToActionNode = new CallToActionNode();

callToActionNode.layout.should.equal('immersive');
callToActionNode.layout.should.equal('minimal');
callToActionNode.layout = 'compact';
callToActionNode.layout.should.equal('compact');

Expand Down
3 changes: 2 additions & 1 deletion packages/koenig-lexical/src/components/ui/cards/CtaCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export function CtaCard({
'py-3': (isEditing || isSelected) && color === 'none' && !hasSponsorLabel,
'pb-3': color === 'none' && !((isEditing || isSelected) && !hasSponsorLabel)
}
)}>
)} data-cta-layout={layout}>
{/* Sponsor label */}
{hasSponsorLabel && (
<div className={clsx(
Expand Down Expand Up @@ -254,6 +254,7 @@ export function CtaCard({
{/* HTML content */}
<KoenigNestedEditor
autoFocus={true}
dataTestId={'cta-card-content-editor'}
hasSettingsPanel={true}
initialEditor={htmlEditor}
initialEditorState={htmlEditorInitialState}
Expand Down
11 changes: 10 additions & 1 deletion packages/koenig-lexical/src/nodes/CallToActionNodeComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ export const CallToActionNodeComponent = ({
node.hasSponsorLabel = !node.hasSponsorLabel;
});
};

const handleBackgroundColorChange = (val) => {
editor.update(() => {
const node = $getNodeByKey(nodeKey);
node.backgroundColor = val;
});
};

const handleUpdatingLayout = (val) => {
editor.update(() => {
const node = $getNodeByKey(nodeKey);
node.layout = val;
});
};

return (
<>
<CtaCard
Expand All @@ -100,9 +108,10 @@ export const CallToActionNodeComponent = ({
updateButtonText={handleButtonTextChange}
updateButtonUrl={handleButtonUrlChange}
updateHasSponsorLabel={handleHasSponsorLabelChange}
updateLayout={() => {}}
updateLayout={handleUpdatingLayout}
updateShowButton={toggleShowButton}
/>

<ActionToolbar
data-kg-card-toolbar="button"
isVisible={showSnippetToolbar}
Expand Down
34 changes: 34 additions & 0 deletions packages/koenig-lexical/test/e2e/cards/cta-card.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,38 @@ test.describe('Call To Action Card', async () => {
await expect(page.locator(firstChildSelector)).toHaveClass(new RegExp(color.expectedClass));
}
});

test('default layout is minimal', async function () {
await focusEditor(page);
await insertCard(page, {cardName: 'call-to-action'});
// find data-cta-layout and check if it data-cta-layout="minimal"
const firstChildSelector = '[data-kg-card="call-to-action"] > :first-child';
await expect(page.locator(firstChildSelector)).toHaveAttribute('data-cta-layout', 'minimal');
expect(await page.getAttribute('[data-testid="cta-card-content-editor"]', 'class')).toContain('text-left');
});

test('can toggle layout to immersive', async function () {
await focusEditor(page);
await insertCard(page, {cardName: 'call-to-action'});
await page.click('[data-testid="immersive-layout"]');
// find data-cta-layout and check if it data-cta-layout="immersive"
const firstChildSelector = '[data-kg-card="call-to-action"] > :first-child';
await expect(page.locator(firstChildSelector)).toHaveAttribute('data-cta-layout', 'immersive');
expect(await page.getAttribute('[data-testid="cta-card-content-editor"]', 'class')).toContain('text-center');
});

test('can toggle layout to immersive and then back to minimal', async function () {
await focusEditor(page);
await insertCard(page, {cardName: 'call-to-action'});
await page.click('[data-testid="immersive-layout"]');
// find data-cta-layout and check if it data-cta-layout="immersive"
const firstChildSelector = '[data-kg-card="call-to-action"] > :first-child';
await expect(page.locator(firstChildSelector)).toHaveAttribute('data-cta-layout', 'immersive');
expect(await page.getAttribute('[data-testid="cta-card-content-editor"]', 'class')).toContain('text-center');

await page.click('[data-testid="minimal-layout"]');
// find data-cta-layout and check if it data-cta-layout="minimal"
await expect(page.locator(firstChildSelector)).toHaveAttribute('data-cta-layout', 'minimal');
expect(await page.getAttribute('[data-testid="cta-card-content-editor"]', 'class')).toContain('text-left');
});
});
Loading