Skip to content
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
59 changes: 50 additions & 9 deletions packages/core/src/lib/components/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,79 @@ import RichText from "./internal/rich-text";

type ToggleProps = {
children?: React.ReactNode;
isOpen?: boolean;
onChangeOpen?: (open: boolean) => void;
} & ToggleArgs;

const Toggle: React.FC<ToggleProps> = ({ children, ...props }) => {
const Toggle: React.FC<ToggleProps> & { Icon: typeof ToggleIcon } = ({
children,
onChangeOpen,
...props
}) => {
const {
toggle: { color, rich_text: texts },
} = props;

const [open, setOpen] = useState(false);

const toggleOpen = useCallback(() => setOpen((prevOpen) => !prevOpen), []);
let iconElement: React.ReactNode = <DefaultToggleIcon open={open} />;
const otherChildren: React.ReactNode[] = [];

React.Children.forEach(children, (child) => {
if (React.isValidElement(child) && child.type === Toggle.Icon) {
iconElement = child.props.children;
} else {
otherChildren.push(child);
}
});

const toggleOpen = useCallback(() => {
setOpen((prevOpen) => {
if (onChangeOpen) {
onChangeOpen(!prevOpen);
}

return !prevOpen;
});
}, [setOpen, onChangeOpen]);

return (
<div
className={`notion-block notion-toggle ${getColorCss(color)} ${open ? "notion-toggle-open" : ""}`}
aria-expanded={open}
>
<div className="notion-toggle-content">
<button onClick={toggleOpen} className="notion-toggle-button">
<div
className={`notion-toggle-button-arrow-box ${open ? "notion-toggle-button-arrow-box-opened" : ""}`}
>
<div className="notion-toggle-button-arrow"></div>
</div>
<button onClick={toggleOpen} className="notion-toggle-button">
{iconElement}
</button>
<p>
<RichText props={texts} />
</p>
</div>

{children}
{otherChildren}
</div>
);
};

type DefaultToggleIconProps = {
open: boolean;
};

const DefaultToggleIcon = ({ open }: DefaultToggleIconProps) => {
return (
<div
className={`notion-toggle-button-arrow-box ${open ? "notion-toggle-button-arrow-box-opened" : ""}`}
>
<div className="notion-toggle-button-arrow"/>
</div>
);
};

const ToggleIcon: React.FC<{ children?: React.ReactNode }> = ({ children }) => (
<>{children}</>
);

Toggle.Icon = ToggleIcon;

export default Toggle;
2 changes: 0 additions & 2 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export type TodoArgs = {
checked: boolean;
rich_text: TextArgs[];
};
customElement?: React.ReactNode;
} & ContextedBlock;

export type ToggleArgs = {
Expand All @@ -110,7 +109,6 @@ export type ToggleArgs = {
color: string;
rich_text: TextArgs[];
};
customElement?: React.ReactNode;
} & ContextedBlock;

export type QuoteArgs = {
Expand Down
3 changes: 2 additions & 1 deletion packages/story/src/stories/todo/todo.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Meta, StoryObj } from "@storybook/react";
import Component from "../../lib/Notion";
import json from "./todo.json";
import { Todo, TodoArgs } from "@notionpresso/react";
import { Todo } from "@notionpresso/react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3: unneccessary change

import { TodoArgs } from "@notionpresso/react";

const blocks = json.blocks as any;

Expand Down
57 changes: 56 additions & 1 deletion packages/story/src/stories/toggle/toggle.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { Meta, StoryObj } from "@storybook/react";
import Component from "../../lib/Notion";
import json from "./toggle.json";

import { Toggle } from "@notionpresso/react";
import type { ToggleArgs } from "@notionpresso/react";
import { useState } from "react";

const blocks = json.blocks as any;

const meta: Meta<typeof Component> = {
Expand All @@ -12,9 +16,60 @@ const meta: Meta<typeof Component> = {
export default meta;
type Story = StoryObj<typeof Component>;

export const Toggle: Story = {
export const ToggleStory: Story = {
name: "Toggle",
args: {
title: "Toggle",
blocks: blocks,
},
};

type ToggleProps = ToggleArgs & {
children?: React.ReactNode;
};

const CustomToggle = ({ children, ...props }: ToggleProps) => {
const [isOpen, setIsOpen] = useState(false);

const handleChangeOpen = (open: boolean) => {
setIsOpen(open);
};

return (
<Toggle {...props} onChangeOpen={handleChangeOpen}>
<Toggle.Icon>
{isOpen ? (
<div
style={{
width: "10px",
height: "3px",
cursor: "pointer",
backgroundColor: "orange",
}}
/>
) : (
<div
style={{
width: "3px",
height: "10px",
cursor: "pointer",
backgroundColor: "orange",
}}
/>
)}
</Toggle.Icon>
{children}
</Toggle>
);
};

export const CustomToggleStory: Story = {
name: "Custom Toggle",
args: {
title: "Custom Toggle",
blocks: blocks,
custom: {
toggle: CustomToggle,
},
},
};