Skip to content

Commit

Permalink
Merge pull request #2895 from Liboul/feat/toggles
Browse files Browse the repository at this point in the history
Feat/toggles
  • Loading branch information
zbeyens authored Jan 31, 2024
2 parents cde7011 + 0d84809 commit aff8e61
Show file tree
Hide file tree
Showing 59 changed files with 1,238 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-comics-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-toggle": minor
---

New plugin: toggle
5 changes: 5 additions & 0 deletions apps/www/content/docs/components/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Use the [CLI](https://platejs.org/docs/components/cli) to install the latest ver

## January 2024 #7

### January 31 #7.5

- add `toggle-element`
- add `toggle-toolbar-button`

### January 11 #7.4

- add support for custom ui dir in `components.json`
Expand Down
63 changes: 63 additions & 0 deletions apps/www/content/docs/components/toggle-element.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: Toggle Element
description: Add toggles to your document.
component: true
docs:
- route: /docs/toggle
title: Toggle
---

## Installation

<Tabs defaultValue="cli">

<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx @udecode/plate-ui@latest add toggle-element
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>

Install the following dependencies:

- [Toggle](/docs/toggle)
- [Toolbar](/docs/components/toolbar)

</Step>

<Step>

Copy and paste the following code into your project.

</Step>

<ComponentSource name="toggle-element" />

<Step>

Update the import paths to match your project setup.

</Step>

</Steps>

</TabsContent>

</Tabs>

## Examples

<ComponentPreview name="playground-demo" id="toggle" />

<ComponentSource src="../../templates/plate-playground-template/src/lib/plate/plate-plugins.ts" />
86 changes: 86 additions & 0 deletions apps/www/content/docs/components/toggle-toolbar-button.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: Toggle Toolbar Button
description: Turns blocks into toggles and vice-versa.
component: true
docs:
- route: /docs/toggle
title: Toggle
- route: /docs/components/toolbar
title: Toolbar
---

## Installation

<Tabs defaultValue="cli">

<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
npx @udecode/plate-ui@latest add toggle-toolbar-button
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>

Install the following dependencies:

- [Toggle](/docs/toggle)
- [Toolbar](/docs/components/toolbar)

</Step>

<Step>

Copy and paste the following code into your project.

</Step>

<ComponentSource name="toggle-toolbar-button" />

<Step>

Update the import paths to match your project setup.

</Step>

</Steps>

</TabsContent>

</Tabs>

<Accordion type="single" collapsible>
<AccordionItem value="manual-installation">
<AccordionTrigger>Manual Installation</AccordionTrigger>
<AccordionContent>

1. Install the following dependencies:

- [Toggle](/docs/toggle)
- [Toolbar](/docs/components/toolbar)

2. Copy and paste the following code into your project.

- `components/plate-ui/toggle-toolbar-button.tsx`

<ComponentSource name="toggle-toolbar-button" />

</AccordionContent>

</AccordionItem>
</Accordion>

## Examples

<ComponentPreview name="playground-demo" id="toggle" />

<ComponentSource src="../../templates/plate-playground-template/src/components/plate-ui/fixed-toolbar-buttons.tsx" />
182 changes: 182 additions & 0 deletions apps/www/content/docs/toggle.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
title: Toggle
description: Add toggles to your document.
docs:
- route: /docs/components/toggle-toolbar-button
title: Toggle Button
---

<ComponentPreview name="playground-demo" id="toggle" />

<PackageInfo>

## Features

- Add toggles to your document.
- Toggles are closed by default, and can be opened to reveal the content inside.
- Refer to the [Indent documentation](/docs/indent) for more information.

## Plugin dependencies

- [Indent](/docs/indent)
- [Node id](/docs/node-id)

</PackageInfo>

## Installation

```bash
npm install @udecode/plate-indent @udecode/plate-node-id @udecode/plate-toggle
```

## Usage

```tsx
// ...
import { createIndentPlugin } from '@udecode/plate-indent';
import { createNodeIdPlugin } from '@udecode/plate-node-id';
import { createTogglePlugin, ELEMENT_TOGGLE } from '@udecode/plate-toggle';

const plugins = [
// ...otherPlugins,
createHeadingPlugin(),
createParagraphPlugin(),
createIndentPlugin({
inject: {
props: {
validTypes: [ELEMENT_TOGGLE, ELEMENT_PARAGRAPH, ELEMENT_H1],
},
},
}),
createNodeIdPlugin(),
createTogglePlugin(),
];
```

## API

### createTogglePlugin

### openNextToggles

Marks the block at the current selection as an open toggle.
Use this function right before inserting a block so that the toggle is expanded upon insertion.

<APIParameters>
<APIItem name="editor" type="TEditor">
The editor instance.
</APIItem>
</APIParameters>

### toggleIds

Toggles the open state of toggles.

<APIParameters>
<APIItem name="editor" type="TEditor">
The editor instance.
</APIItem>
<APIItem name="toggleIds" type="string[]">
An array of element ids.
</APIItem>
<APIItem name="force" type="boolean" optional>
Set to true if you want to expand all toggles regardless of their current
state. Set to false if you want to collapse all toggles regardless of their
current state.
</APIItem>
</APIParameters>

## API Components

### useToggleToolbarButtonState

<APIReturns>
<APIItem name="pressed" type="boolean">
A boolean indicating whether the button is pressed or not.
</APIItem>
</APIReturns>

### useToggleToolbarButton

A behavior hook for the toggle toolbar button.

<APIState>
<APIItem name="pressed" type="boolean">
A boolean indicating whether the button is pressed or not.
</APIItem>
</APIState>

<APIReturns>
<APIItem name="props" type="object">
<APISubList>
<APISubListItem parent="props" name="pressed" type="boolean">
A boolean indicating whether the button is pressed or not.
</APISubListItem>
<APISubListItem parent="props" name="onClick" type="function">
A callback function to handle the click event of the button. It toggles
the toggle of the specified node type in the editor and focuses the
editor.
</APISubListItem>
<APISubListItem parent="props" name="onMouseDown" type="function">
A callback function to handle the mouse down event of the button. It
just prevents default so that focus is not lost when clicking on the
button.
</APISubListItem>
</APISubList>
</APIItem>
</APIReturns>

### useToggleButtonState

<APIParameters>
<APIItem name="toggleId" type="string">
The id of the toggle element.
</APIItem>
</APIParameters>

<APIReturns>
<APIItem name="toggleId" type="string">
The id of the toggle element.
</APIItem>
<APIItem name="open" type="boolean">
A boolean indicating whether the toggle is open (expanded) or not
(collapsed).
</APIItem>
</APIReturns>

### useToggleButton

A behavior hook for the toggle button that expands or collapses a toggle element.

<APIParameters>
<APIItem name="toggleId" type="string">
The id of the toggle element.
</APIItem>
<APIItem name="open" type="boolean">
A boolean indicating whether the toggle is open (expanded) or not
(collapsed).
</APIItem>
</APIParameters>

<APIReturns>
<APIItem name="toggleId" type="string">
The id of the toggle element.
</APIItem>
<APIItem name="open" type="boolean">
A boolean indicating whether the toggle is open (expanded) or not
(collapsed).
</APIItem>
<APIItem name="buttonProps" type="object">
<APISubList>
<APISubListItem parent="buttonProps" name="onClick" type="function">
A callback function to handle the click event of the button. It toggles
the open state of the toggle.
</APISubListItem>
<APISubListItem parent="buttonProps" name="onMouseDown" type="function">
A callback function to handle the mouse down event of the button. It
just prevents default so that focus is not lost when clicking on the
button.
</APISubListItem>
</APISubList>
</APIItem>
</APIReturns>
1 change: 1 addition & 0 deletions apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@udecode/plate-tabbable": "workspace:^",
"@udecode/plate-table": "workspace:^",
"@udecode/plate-test-utils": "workspace:^",
"@udecode/plate-toggle": "workspace:^",
"@udecode/plate-trailing-block": "workspace:^",
"@udecode/plate-utils": "workspace:^",
"@udecode/react-utils": "workspace:^",
Expand Down
24 changes: 24 additions & 0 deletions apps/www/public/registry/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,30 @@
],
"type": "components:plate-ui"
},
{
"name": "toggle-element",
"dependencies": [
"@udecode/plate-toggle"
],
"registryDependencies": [],
"files": [
"plate-ui/toggle-element.tsx"
],
"type": "components:plate-ui"
},
{
"name": "toggle-toolbar-button",
"dependencies": [
"@udecode/plate-toggle"
],
"registryDependencies": [
"toolbar"
],
"files": [
"plate-ui/toggle-toolbar-button.tsx"
],
"type": "components:plate-ui"
},
{
"name": "toolbar",
"dependencies": [
Expand Down
Loading

0 comments on commit aff8e61

Please sign in to comment.