From 19e65d2c872207a6a2e089270cd994819bcba0ac Mon Sep 17 00:00:00 2001 From: Albert Yu Date: Tue, 29 Oct 2024 00:49:12 +0800 Subject: [PATCH 01/19] Add ToggleButton component and hooks --- docs/data/api/toggle-button.json | 29 ++++ .../toggle-button/ToggleButtonIntroduction.js | 26 ++++ .../ToggleButtonIntroduction.tsx | 26 ++++ .../ToggleButtonIntroduction.tsx.preview | 16 ++ .../toggle-button/styles.module.css | 38 +++++ .../toggle-button/toggle-button.mdx | 76 ++++++++++ docs/data/pages.ts | 1 + .../api-docs/toggle-button/toggle-button.json | 26 ++++ docs/src/app/experiments/toggle.tsx | 30 ++++ .../src/ToggleButton/ToggleButton.test.tsx | 101 +++++++++++++ .../src/ToggleButton/ToggleButton.tsx | 142 ++++++++++++++++++ packages/mui-base/src/ToggleButton/index.ts | 1 + .../src/ToggleButton/useToggleButton.ts | 112 ++++++++++++++ packages/mui-base/src/index.ts | 1 + 14 files changed, 625 insertions(+) create mode 100644 docs/data/api/toggle-button.json create mode 100644 docs/data/components/toggle-button/ToggleButtonIntroduction.js create mode 100644 docs/data/components/toggle-button/ToggleButtonIntroduction.tsx create mode 100644 docs/data/components/toggle-button/ToggleButtonIntroduction.tsx.preview create mode 100644 docs/data/components/toggle-button/styles.module.css create mode 100644 docs/data/components/toggle-button/toggle-button.mdx create mode 100644 docs/data/translations/api-docs/toggle-button/toggle-button.json create mode 100644 docs/src/app/experiments/toggle.tsx create mode 100644 packages/mui-base/src/ToggleButton/ToggleButton.test.tsx create mode 100644 packages/mui-base/src/ToggleButton/ToggleButton.tsx create mode 100644 packages/mui-base/src/ToggleButton/index.ts create mode 100644 packages/mui-base/src/ToggleButton/useToggleButton.ts diff --git a/docs/data/api/toggle-button.json b/docs/data/api/toggle-button.json new file mode 100644 index 0000000000..bd65e37c6b --- /dev/null +++ b/docs/data/api/toggle-button.json @@ -0,0 +1,29 @@ +{ + "props": { + "aria-label": { "type": { "name": "string" } }, + "aria-labelledby": { "type": { "name": "string" } }, + "className": { "type": { "name": "union", "description": "func
| string" } }, + "defaultPressed": { "type": { "name": "bool" }, "default": "false" }, + "disabled": { "type": { "name": "bool" }, "default": "false" }, + "onPressedChange": { + "type": { "name": "func" }, + "signature": { + "type": "function(pressed: boolean, event: Event) => void", + "describedArgs": ["pressed", "event"] + } + }, + "pressed": { "type": { "name": "bool" }, "default": "undefined" }, + "render": { "type": { "name": "union", "description": "element
| func" } } + }, + "name": "ToggleButton", + "imports": ["import { ToggleButton } from '@base_ui/react/ToggleButton';"], + "classes": [], + "spread": true, + "themeDefaultProps": true, + "muiName": "ToggleButton", + "forwardsRefTo": "HTMLButtonElement", + "filename": "/packages/mui-base/src/ToggleButton/ToggleButton.tsx", + "inheritance": null, + "demos": "", + "cssComponent": false +} diff --git a/docs/data/components/toggle-button/ToggleButtonIntroduction.js b/docs/data/components/toggle-button/ToggleButtonIntroduction.js new file mode 100644 index 0000000000..446269b0ca --- /dev/null +++ b/docs/data/components/toggle-button/ToggleButtonIntroduction.js @@ -0,0 +1,26 @@ +'use client'; +import * as React from 'react'; +import { ToggleButton } from '@base_ui/react/ToggleButton'; +import classes from './styles.module.css'; + +export default function ToggleButtonIntroduction() { + const [pressed, setPressed] = React.useState(false); + return ( + + + + + + ); +} diff --git a/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx b/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx new file mode 100644 index 0000000000..446269b0ca --- /dev/null +++ b/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx @@ -0,0 +1,26 @@ +'use client'; +import * as React from 'react'; +import { ToggleButton } from '@base_ui/react/ToggleButton'; +import classes from './styles.module.css'; + +export default function ToggleButtonIntroduction() { + const [pressed, setPressed] = React.useState(false); + return ( + + + + + + ); +} diff --git a/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx.preview b/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx.preview new file mode 100644 index 0000000000..3109ca040b --- /dev/null +++ b/docs/data/components/toggle-button/ToggleButtonIntroduction.tsx.preview @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/docs/data/components/toggle-button/styles.module.css b/docs/data/components/toggle-button/styles.module.css new file mode 100644 index 0000000000..13cf86e1d1 --- /dev/null +++ b/docs/data/components/toggle-button/styles.module.css @@ -0,0 +1,38 @@ +.button { + --size: 2.5rem; + --corner: 0.4rem; + + display: flex; + flex-flow: row nowrap; + justify-content: center; + align-items: center; + height: var(--size); + width: var(--size); + border: 1px solid var(--gray-outline-2); + border-radius: var(--corner); + background-color: var(--gray-container-1); + color: var(--gray-text-1); +} + +.button:hover { + background-color: var(--gray-surface-1); + color: var(--gray-text-2); +} + +.button:focus-visible { + outline: 2px solid var(--gray-900); +} + +.icon { + fill: none; + stroke: currentColor; + stroke-width: 2; + stroke-linecap: round; + stroke-linejoin: round; +} + +.button[data-pressed] .icon { + color: var(--gray-text-2); + fill: currentColor; + stroke-width: 0; +} diff --git a/docs/data/components/toggle-button/toggle-button.mdx b/docs/data/components/toggle-button/toggle-button.mdx new file mode 100644 index 0000000000..23fcf639ea --- /dev/null +++ b/docs/data/components/toggle-button/toggle-button.mdx @@ -0,0 +1,76 @@ +--- +productId: base-ui +title: React ToggleButton component +description: Toggle Buttons are a two-state button that can either be off (not pressed) or on (pressed). +components: ToggleButton +hooks: useToggleButton +githubLabel: 'component: toggle button' +waiAria: https://www.w3.org/WAI/ARIA/apg/patterns/button/ +packageName: '@base_ui/react' +--- + +# ToggleButton + + + + + + + +## Installation + + + +## Anatomy + +ToggleButton is a single component that renders a `