diff --git a/.dumirc.ts b/.dumirc.ts index ebd40b0d..50c681a4 100644 --- a/.dumirc.ts +++ b/.dumirc.ts @@ -1,17 +1,19 @@ // more config: https://d.umijs.org/config import { defineConfig } from 'dumi'; +const basePath = process.env.GH_PAGES ? '/menu/' : '/'; +const publicPath = basePath; + export default defineConfig({ themeConfig: { - name: 'rc-menu', + name: 'Menu', logo: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4', - nav: [ - { title: 'Demo', link: '/demo/antd'} - ], + nav: [{ title: 'Demo', link: '/demo/antd' }], }, - favicons: - ['https://avatars0.githubusercontent.com/u/9441414?s=200&v=4'], - outputPath: '.doc', + favicons: ['https://avatars0.githubusercontent.com/u/9441414?s=200&v=4'], + outputPath: 'docs-dist', + base: basePath, + publicPath, exportStatic: {}, mfsu: {}, styles: [ @@ -20,5 +22,5 @@ export default defineConfig({ width: auto !important; } `, - ] + ], }); diff --git a/.fatherrc.ts b/.fatherrc.ts new file mode 100644 index 00000000..96268ae1 --- /dev/null +++ b/.fatherrc.ts @@ -0,0 +1,5 @@ +import { defineConfig } from 'father'; + +export default defineConfig({ + plugins: ['@rc-component/father-plugin'], +}); diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 33b1999c..758659af 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,15 +1,2 @@ -# These are supported funding model platforms - -github: ant-design # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -patreon: # Replace with a single Patreon username -open_collective: ant-design # Replace with a single Open Collective username -ko_fi: # Replace with a single Ko-fi username -tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -liberapay: # Replace with a single Liberapay username -issuehunt: # Replace with a single IssueHunt username -lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry -polar: # Replace with a single Polar username -buy_me_a_coffee: # Replace with a single Buy Me a Coffee username -thanks_dev: # Replace with a single thanks.dev username -custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] +github: ant-design +open_collective: ant-design diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 6745ced9..3b730ef9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,28 +1,19 @@ version: 2 updates: -- package-ecosystem: npm - directory: "/" - schedule: - interval: daily - time: "21:00" - open-pull-requests-limit: 10 - ignore: - - dependency-name: "@types/react-dom" - versions: - - 17.0.0 - - 17.0.1 - - 17.0.2 - - dependency-name: "@types/react" - versions: - - 17.0.0 - - 17.0.1 - - 17.0.2 - - 17.0.3 - - dependency-name: np - versions: - - 7.2.0 - - 7.3.0 - - 7.4.0 - - dependency-name: less - versions: - - 4.1.0 + - package-ecosystem: npm + directory: '/' + schedule: + interval: weekly + day: monday + time: '21:00' + timezone: Asia/Shanghai + open-pull-requests-limit: 10 + + - package-ecosystem: github-actions + directory: '/' + schedule: + interval: weekly + day: monday + time: '21:00' + timezone: Asia/Shanghai + open-pull-requests-limit: 10 diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 5efca0f4..984e4786 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -1,12 +1,12 @@ -name: "CodeQL" +name: 'CodeQL' on: push: - branches: [ "master" ] + branches: ['master'] pull_request: - branches: [ "master" ] + branches: ['master'] schedule: - - cron: "12 22 * * 6" + - cron: '12 22 * * 6' jobs: analyze: @@ -20,22 +20,24 @@ jobs: strategy: fail-fast: false matrix: - language: [ javascript ] + language: [javascript] steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v7 + with: + persist-credentials: false - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@8aad20d150bbac5944a9f9d289da16a4b0d87c1e with: languages: ${{ matrix.language }} queries: +security-and-quality - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@8aad20d150bbac5944a9f9d289da16a4b0d87c1e - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@8aad20d150bbac5944a9f9d289da16a4b0d87c1e with: - category: "/language:${{ matrix.language }}" + category: '/language:${{ matrix.language }}' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 5735e2d2..00000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,6 +0,0 @@ -name: ✅ test -on: [push, pull_request] -jobs: - test: - uses: react-component/rc-test/.github/workflows/test.yml@main - secrets: inherit \ No newline at end of file diff --git a/.github/workflows/react-component-ci.yml b/.github/workflows/react-component-ci.yml new file mode 100644 index 00000000..9503b960 --- /dev/null +++ b/.github/workflows/react-component-ci.yml @@ -0,0 +1,8 @@ +name: ✅ test +on: [push, pull_request] +permissions: + contents: read +jobs: + test: + uses: react-component/rc-test/.github/workflows/test-utoo.yml@main + secrets: inherit diff --git a/.github/workflows/react-doctor.yml b/.github/workflows/react-doctor.yml new file mode 100644 index 00000000..097eb883 --- /dev/null +++ b/.github/workflows/react-doctor.yml @@ -0,0 +1,27 @@ +name: React Doctor + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + push: + branches: [master] + +permissions: + contents: read + pull-requests: write + issues: write + statuses: write + +concurrency: + group: react-doctor-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + react-doctor: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + fetch-depth: 0 + persist-credentials: false + - uses: millionco/react-doctor@0b4f4f4bd248a154e64eb508a48347f71154b3f3 diff --git a/.github/workflows/surge-preview.yml b/.github/workflows/surge-preview.yml new file mode 100644 index 00000000..4c2f17ed --- /dev/null +++ b/.github/workflows/surge-preview.yml @@ -0,0 +1,54 @@ +name: Surge Preview + +on: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +permissions: + contents: read + pull-requests: write + checks: write + +jobs: + preview: + runs-on: ubuntu-latest + concurrency: + group: surge-preview-${{ github.event.pull_request.number }} + cancel-in-progress: true + env: + PREVIEW: true + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - name: Check Surge token + id: surge-token + env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + run: | + if [ -n "$SURGE_TOKEN" ]; then + echo "enabled=true" >> "$GITHUB_OUTPUT" + else + echo "enabled=false" >> "$GITHUB_OUTPUT" + fi + - name: Build preview + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + run: | + npm install + npm run build + - uses: afc163/surge-preview@bf90a5a86111f6311ca42f0a5a0f80fb0fb03cec + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + with: + surge_token: ${{ env.SURGE_TOKEN }} + github_token: ${{ secrets.GITHUB_TOKEN }} + dist: docs-dist + failOnError: false + setCommitStatus: false + - name: Skip Surge preview + if: ${{ steps.surge-token.outputs.enabled != 'true' }} + run: echo "SURGE_TOKEN is not configured; skip Surge preview." diff --git a/.gitignore b/.gitignore index 898129e9..267c6ce6 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,8 @@ build lib es coverage +.doc +docs-dist/ yarn.lock package-lock.json pnpm-lock.yaml @@ -38,4 +40,5 @@ pnpm-lock.yaml .dumi/tmp-production .env.local -bun.lockb \ No newline at end of file +bun.lockb +.vercel diff --git a/.husky/pre-commit b/.husky/pre-commit index c27d8893..2312dc58 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -lint-staged +npx lint-staged diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..c466d872 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,14 @@ +node_modules +coverage +docs-dist +dist +es +lib +.dumi/tmp +.dumi/tmp-production +.vercel +package-lock.json +pnpm-lock.yaml +yarn.lock +bun.lockb +*.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..bd0a1f72 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019-present react-component + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 22d3dce2..ac8052cc 100644 --- a/README.md +++ b/README.md @@ -1,464 +1,184 @@ -# @rc-component/menu - ---- - -React Menu Component. port from https://github.com/kissyteam/menu - -[![NPM version][npm-image]][npm-url] [![npm download][download-image]][download-url] [![build status][github-actions-image]][github-actions-url] [![Codecov][codecov-image]][codecov-url] [![bundle size][bundlephobia-image]][bundlephobia-url] [![dumi][dumi-image]][dumi-url] - -[npm-image]: https://img.shields.io/npm/v/@rc-component/menu.svg?style=flat-square -[npm-url]: https://npmjs.org/package/@rc-component/menu -[github-actions-image]: https://github.com/react-component/menu/actions/workflows/main.yml/badge.svg -[github-actions-url]: https://github.com/react-component/menu/actions/workflows/main.yml -[circleci-image]: https://img.shields.io/circleci/react-component/menu/master?style=flat-square -[circleci-url]: https://circleci.com/gh/react-component/menu -[codecov-image]: https://img.shields.io/codecov/c/github/react-component/footer/master.svg?style=flat-square -[codecov-url]: https://codecov.io/gh/react-component/footer/branch/master -[coveralls-image]: https://img.shields.io/coveralls/react-component/menu.svg?style=flat-square -[coveralls-url]: https://coveralls.io/r/react-component/menu?branch=master -[david-url]: https://david-dm.org/react-component/menu -[david-image]: https://david-dm.org/react-component/menu/status.svg?style=flat-square -[david-dev-url]: https://david-dm.org/react-component/menu?type=dev -[david-dev-image]: https://david-dm.org/react-component/menu/dev-status.svg?style=flat-square -[download-image]: https://img.shields.io/npm/dm/@rc-component/menu.svg?style=flat-square -[download-url]: https://npmjs.org/package/@rc-component/menu -[bundlephobia-url]: https://bundlephobia.com/result?p=@rc-component/menu -[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/@rc-component/menu -[dumi-url]: https://github.com/umijs/dumi -[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square +
+

@rc-component/menu

+

Ant Design Part of the Ant Design ecosystem.

+

🧭 Accessible React menu primitives for navigation, command surfaces, and nested item trees.

+ +

+ NPM version + npm downloads + build status + Codecov + bundle size + dumi +

+
+ +

English | 简体中文

+ +## Highlights + +- Horizontal, vertical, and inline menu modes. +- Controlled and uncontrolled selection, open keys, and active key state. +- `items` configuration API with legacy children support. +- Sub menus, item groups, dividers, icons, overflow, popup rendering, and keyboard focus helpers. +- TypeScript definitions and semantic `classNames` / `styles` slots. +- Used by Ant Design as the shared menu foundation. ## Install -[![@rc-component/menu](https://nodei.co/npm/@rc-component/menu.png)](https://npmjs.org/package/@rc-component/menu) +```bash +npm install @rc-component/menu +``` ## Usage -```js -import Menu, { SubMenu, MenuItem } from '@rc-component/menu'; +```tsx | pure +import Menu, { type MenuProps } from '@rc-component/menu'; + +const items: MenuProps['items'] = [ + { key: 'home', label: 'Home' }, + { + key: 'docs', + label: 'Docs', + children: [ + { key: 'quick-start', label: 'Quick start' }, + { key: 'api', label: 'API' }, + ], + }, +]; + +export default () => ( + +); +``` + +```tsx | pure +import Menu, { MenuItem, SubMenu } from '@rc-component/menu'; -ReactDOM.render( +export default () => ( - 1 - - 2-1 + Item 1 + + Item 2-1 - , - container, + ); ``` -## Compatibility +## Examples -| [IE / Edge](http://godban.github.io/browsers-support-badges/)
IE / Edge | [Firefox](http://godban.github.io/browsers-support-badges/)
Firefox | [Chrome](http://godban.github.io/browsers-support-badges/)
Chrome | [Safari](http://godban.github.io/browsers-support-badges/)
Safari | [Electron](http://godban.github.io/browsers-support-badges/)
Electron | -| --- | --- | --- | --- | --- | -| IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions | +Run the local dumi site: -## API - -### Menu props - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nametypedefaultdescription
refReact.HTMLLIElementget dom node
classNameStringadditional css class of root dom node
modeStringverticalone of ["horizontal","inline","vertical-left","vertical-right"]
activeKeyStringinitial and current active menu item's key.
defaultActiveFirstBooleanfalsewhether active first menu item when show if activeKey is not set or invalid
multipleBooleanfalsewhether allow multiple select
selectableBooleantrueallow selecting menu items
selectedKeysString[][]selected keys of items
defaultSelectedKeysString[][]initial selected keys of items
openKeysString[][]open keys of SubMenuItem
defaultOpenKeysString[][]initial open keys of SubMenuItem
onSelectfunction({key:String, item:ReactComponent, domEvent:Event, selectedKeys:String[],ItemData:ItemData})called when select a menu item
onClickfunction({key:String, item:ReactComponent, domEvent:Event, keyPath: String[], ItemData:ItemData})called when click a menu item
onOpenChange(openKeys:String[]) => voidcalled when open/close sub menu
onDeselectfunction({key:String, item:ReactComponent, domEvent:Event, selectedKeys:String[]})called when deselect a menu item. only called when allow multiple
triggerSubMenuActionEnum { hover, click }hoverwhich action can trigger submenu open/close
openAnimation{enter:function,leave:function}|Stringanimate when sub menu open or close. see @rc-component/motion for object type.
openTransitionStringcss transitionName when sub menu open or close
subMenuOpenDelayNumber0delay time to show popup sub menu. unit: s
subMenuCloseDelayNumber0.1delay time to hide popup sub menu. unit: s
forceSubMenuRenderBooleanfalsewhether to render submenu even if it is not visible
getPopupContainerFunction(menuDOMNode): HTMLElement() => document.bodyWhere to render the DOM node of popup menu when the mode is horizontal or vertical
builtinPlacementsObject of alignConfigs for dom-alignsee placements.tsDescribes how the popup menus should be positioned
itemIconReactNode | (props: MenuItemProps) => ReactNodeSpecify the menu item icon.
expandIconReactNode | (props: SubMenuProps & { isSubMenu: boolean }) => ReactNodeSpecify the menu item icon.
directionStringLayout direction of menu component, it supports RTL direction too.
inlineIndentNumber24Padding level multiplier. Right or left padding depends on param "direction".
- -### Menu.Item props - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nametypedefaultdescription
classNameStringadditional css class of root dom node
disabledBooleanfalseno effect for click or keydown for this item
extraReact.ReactNodeSpecify menu item extra node.
keyObjectcorresponding to activeKey
onMouseEnterFunction({eventKey, domEvent})
onMouseLeaveFunction({eventKey, domEvent})
itemIconReactNode | (props: MenuItemProps) => ReactNodeSpecify the menu item icon.
- -### Menu.SubMenu props +```bash +npm install +npm start +``` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nametypedefaultdescription
refReact.HTMLLIElementget dom node
popupClassNameStringadditional css class of root dom node
popupStyleCSSPropertiesadditional css style of root dom node
titleString/ReactElementsub menu's content
overflowedIndicatorString/ReactElement···overflow indicator when menu overlows in horizontal mode
keyObjectcorresponding to activeKey
disabledBooleanfalseno effect for click or keydown for this item
onMouseEnterFunction({eventKey, domEvent})
onMouseLeaveFunction({eventKey, domEvent})
onTitleMouseEnterFunction({eventKey, domEvent})
onTitleMouseLeaveFunction({eventKey, domEvent})
onTitleClickFunction({eventKey, domEvent})
popupOffsetArrayThe offset of the popup submenu, in an x, y coordinate array. e.g.: `[0,15]`
expandIconReactNode | (props: SubMenuProps) => ReactNodeSpecify the menu item icon.
itemIconReactNode | (props: SubMenuProps & { isSubMenu: boolean }) => ReactNodeSpecify the menu item icon.
+Then open `http://localhost:8000`. -### Menu.Divider props +## API -none +### Menu + +| Property | Type | Default | Description | +| --- | --- | --- | --- | +| activeKey | `string` | - | Controlled active item key. | +| builtinPlacements | `Record` | - | Popup alignment placements for sub menus. | +| className | `string` | - | Class name for the root menu. | +| classNames | `Partial>` | - | Semantic class names for menu slots. | +| defaultActiveFirst | `boolean` | `false` | Focus the first enabled item when active key is absent. | +| defaultOpenKeys | `string[]` | `[]` | Initial open sub menu keys. | +| defaultSelectedKeys | `string[]` | `[]` | Initial selected item keys. | +| defaultMotions | `Partial>` | - | Motion config by menu mode. | +| direction | `'ltr' \| 'rtl'` | - | Layout direction. | +| disabled | `boolean` | `false` | Disable all menu interactions. | +| disabledOverflow | `boolean` | `false` | Disable overflow measurement. | +| expandIcon | `ReactNode \| (props: RenderIconInfo) => ReactNode` | - | Custom sub menu expand icon. | +| forceSubMenuRender | `boolean` | `false` | Render popup sub menus before they are opened. | +| getPopupContainer | `(node: HTMLElement) => HTMLElement` | - | Container for popup sub menus. | +| inlineCollapsed | `boolean` | - | Collapse inline menu layout. | +| inlineIndent | `number` | `24` | Indent width for inline mode. | +| itemIcon | `ReactNode \| (props: RenderIconInfo) => ReactNode` | - | Custom item icon. | +| items | `ItemType[]` | - | Menu item configuration. | +| mode | `'horizontal' \| 'vertical' \| 'inline'` | `vertical` | Menu display mode. | +| motion | `CSSMotionProps` | - | Motion config for menu transitions. | +| multiple | `boolean` | `false` | Allow multiple selected items. | +| openKeys | `string[]` | - | Controlled open sub menu keys. | +| overflowedIndicator | `ReactNode` | `"..."` | Indicator rendered for overflowed items. | +| popupRender | `(node, info) => ReactNode` | - | Customize popup menu rendering. | +| prefixCls | `string` | `rc-menu` | Class name prefix. | +| rootClassName | `string` | - | Class name for the root wrapper. | +| selectable | `boolean` | `true` | Allow item selection. | +| selectedKeys | `string[]` | - | Controlled selected item keys. | +| styles | `Partial>` | - | Semantic styles for menu slots. | +| subMenuCloseDelay | `number` | `0.1` | Delay in seconds before closing popup sub menus. | +| subMenuOpenDelay | `number` | `0.1` | Delay in seconds before opening popup sub menus. | +| triggerSubMenuAction | `'click' \| 'hover'` | `hover` | Interaction that opens sub menus. | +| onClick | `(info: MenuInfo) => void` | - | Triggered when an item is clicked. | +| onDeselect | `(info: SelectInfo) => void` | - | Triggered when an item is deselected. | +| onOpenChange | `(openKeys: string[]) => void` | - | Triggered when open keys change. | +| onSelect | `(info: SelectInfo) => void` | - | Triggered when an item is selected. | + +### ItemType + +```ts +type ItemType = + | { + type?: 'item'; + // Item keys accept React.Key and are normalized to strings in event info. + key: React.Key; + label?: React.ReactNode; + disabled?: boolean; + itemIcon?: RenderIconType; + extra?: React.ReactNode; + } + | { + type?: 'submenu'; + // Sub menu keys are strings to match openKeys/defaultOpenKeys. + key: string; + label?: React.ReactNode; + children: ItemType[]; + disabled?: boolean; + } + | { + type: 'group'; + label?: React.ReactNode; + children?: ItemType[]; + } + | { + type: 'divider'; + }; +``` -### Menu.ItemGroup props +### Ref - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nametypedefaultdescription
refReact.HTMLLIElementget dom node
titleString|React.Elementtitle of item group
childrenReact.Element[]MenuItems belonged to this group
+| Method | Type | Description | +| --- | --- | --- | +| `focus` | `(options?: FocusOptions) => void` | Focus the active item or first enabled item. | +| `findItem` | `({ key: string }) => HTMLElement \| null` | Find the DOM element for an item key. | +| `list` | `HTMLUListElement` | Root menu list element. | ## Development -``` +```bash npm install npm start -``` - -## Example - -http://localhost:8001/examples/index.md - -online example: http://react-component.github.io/menu/examples/ - -## Test Case - -``` npm test -npm run chrome-test +npm run tsc +npm run compile +npm run build ``` -## Coverage +The dumi site runs at `http://localhost:8000` by default. -``` -npm run coverage +## Release + +```bash +npm run prepublishOnly ``` -open coverage/ dir +The release flow is handled by `@rc-component/np` through the `rc-np` command after the package build. ## License -@rc-component/menu is released under the MIT license. +@rc-component/menu is released under the [MIT](./LICENSE) license. diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100644 index 00000000..cfb19eb7 --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,184 @@ +
+

@rc-component/menu

+

Ant Design Ant Design 生态的一部分。

+

🧭 React 菜单组件,支持水平、垂直、内联、分组和子菜单。

+ +

+ NPM version + npm downloads + build status + Codecov + bundle size + dumi +

+
+ +

English | 简体中文

+ +## 特性 + +- 水平、垂直和内联菜单模式。 +- 受控和非受控选择、打开键和活动键状态。 +- `items` 配置 API 具有旧子支持。 +- 子菜单、项目组、分隔线、图标、溢出、弹层渲染和键盘焦点助手。 +- 提供 TypeScript 类型定义和语义化 `classNames` / `styles` 插槽。 +- 被 Ant Design 用作共享的 menu 基础能力。 + +## 安装 + +```bash +npm install @rc-component/menu +``` + +## 使用 + +```tsx | pure +import Menu, { type MenuProps } from '@rc-component/menu'; + +const items: MenuProps['items'] = [ + { key: 'home', label: 'Home' }, + { + key: 'docs', + label: 'Docs', + children: [ + { key: 'quick-start', label: 'Quick start' }, + { key: 'api', label: 'API' }, + ], + }, +]; + +export default () => ( + +); +``` + +```tsx | pure +import Menu, { MenuItem, SubMenu } from '@rc-component/menu'; + +export default () => ( + + Item 1 + + Item 2-1 + + +); +``` + +## 示例 + +运行本地 dumi 站点: + +```bash +npm install +npm start +``` + +然后打开 `http://localhost:8000`。 + +## API + +### Menu + +| 参数 | 类型 | 默认值 | 说明 | +| --- | --- | --- | --- | +| activeKey | `string` | - | 受控活动项目键。 | +| builtinPlacements | `Record` | - | 子菜单的弹层对齐位置。 | +| className | `string` | - | 根菜单的 className。 | +| classNames | `Partial>` | - | 菜单槽的语义 className。 | +| defaultActiveFirst | `boolean` | `false` | 当活动键不存在时,聚焦第一个启用的项目。 | +| defaultOpenKeys | `string[]` | `[]` | 初始打开子菜单键。 | +| defaultSelectedKeys | `string[]` | `[]` | 初始选定项目键。 | +| defaultMotions | `Partial>` | - | 通过菜单模式进行运动配置。 | +| direction | `'ltr' \| 'rtl'` | - | 布局方向。 | +| disabled | `boolean` | `false` | 禁用所有菜单交互。 | +| disabledOverflow | `boolean` | `false` | 禁用溢出测量。 | +| expandIcon | `ReactNode \| (props: RenderIconInfo) => ReactNode` | - | 自定义子菜单展开图标。 | +| forceSubMenuRender | `boolean` | `false` | 在打开弹层子菜单之前渲染它们。 | +| getPopupContainer | `(node: HTMLElement) => HTMLElement` | - | 弹层子菜单的容器。 | +| inlineCollapsed | `boolean` | - | 折叠内联菜单布局。 | +| inlineIndent | `number` | `24` | 内联模式的缩进宽度。 | +| itemIcon | `ReactNode \| (props: RenderIconInfo) => ReactNode` | - | 自定义项目图标。 | +| items | `ItemType[]` | - | 菜单项配置。 | +| mode | `'horizontal' \| 'vertical' \| 'inline'` | `vertical` | 菜单展示模式。 | +| motion | `CSSMotionProps` | - | 菜单转换的运动配置。 | +| multiple | `boolean` | `false` | 允许选择多个项目。 | +| openKeys | `string[]` | - | 受控打开子菜单键。 | +| overflowedIndicator | `ReactNode` | `"..."` | 为溢出项目呈现的指示器。 | +| popupRender | `(node, info) => ReactNode` | - | 自定义弹层菜单渲染。 | +| prefixCls | `string` | `rc-menu` | className 前缀。 | +| rootClassName | `string` | - | 根包装器的 className。 | +| selectable | `boolean` | `true` | 允许选择项目。 | +| selectedKeys | `string[]` | - | 控制选定的项目键。 | +| styles | `Partial>` | - | 菜单槽的语义样式。 | +| subMenuCloseDelay | `number` | `0.1` | 关闭弹层子菜单之前延迟几秒。 | +| subMenuOpenDelay | `number` | `0.1` | 打开弹层子菜单之前延迟几秒。 | +| triggerSubMenuAction | `'click' \| 'hover'` | `hover` | 打开子菜单的交互。 | +| onClick | `(info: MenuInfo) => void` | - | 单击某个项目时触发。 | +| onDeselect | `(info: SelectInfo) => void` | - | 取消选择某个项目时触发。 | +| onOpenChange | `(openKeys: string[]) => void` | - | 当打开键更改时触发。 | +| onSelect | `(info: SelectInfo) => void` | - | 选择项目时触发。 | + +### ItemType + +```ts +type ItemType = + | { + type?: 'item'; + // Item keys accept React.Key and are normalized to strings in event info. + key: React.Key; + label?: React.ReactNode; + disabled?: boolean; + itemIcon?: RenderIconType; + extra?: React.ReactNode; + } + | { + type?: 'submenu'; + // Sub menu keys are strings to match openKeys/defaultOpenKeys. + key: string; + label?: React.ReactNode; + children: ItemType[]; + disabled?: boolean; + } + | { + type: 'group'; + label?: React.ReactNode; + children?: ItemType[]; + } + | { + type: 'divider'; + }; +``` + +### Ref + +| Method | 类型 | 说明 | +| ---------- | ------------------------------------------ | -------------------------------- | +| `focus` | `(options?: FocusOptions) => void` | 聚焦活动项目或第一个启用的项目。 | +| `findItem` | `({ key: string }) => HTMLElement \| null` | 查找项目键的 DOM 元素。 | +| `list` | `HTMLUListElement` | 根菜单列表元素。 | + +## 本地开发 + +```bash +npm install +npm start +npm test +npm run tsc +npm run compile +npm run build +``` + +dumi 站点默认运行在 `http://localhost:8000`。 + +## 发布 + +```bash +npm run prepublishOnly +``` + +包构建完成后,发布流程由 `@rc-component/np` 通过 `rc-np` 命令处理。 + +## 许可证 + +@rc-component/menu 基于 [MIT](./LICENSE) 许可证发布。 diff --git a/now.json b/now.json deleted file mode 100644 index 2f8b73fd..00000000 --- a/now.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "version": 2, - "name": "rc-menu", - "builds": [ - { - "src": "package.json", - "use": "@now/static-build", - "config": { "distDir": ".doc" } - } - ], - "routes": [ - { "src": "/(.*)", "dest": "/dist/$1" } - ] -} diff --git a/package.json b/package.json index 491a6bd6..3fe3f9d4 100644 --- a/package.json +++ b/package.json @@ -9,13 +9,13 @@ "ui", "react-menu" ], - "homepage": "http://github.com/react-component/menu", + "homepage": "https://react-component.github.io/menu", "bugs": { - "url": "http://github.com/react-component/menu/issues" + "url": "https://github.com/react-component/menu/issues" }, "repository": { "type": "git", - "url": "git@github.com:react-component/menu.git" + "url": "https://github.com/react-component/menu.git" }, "license": "MIT", "maintainers": [ @@ -31,16 +31,18 @@ "assets/*.less" ], "scripts": { + "build": "npm run docs:build", "compile": "father build && lessc assets/index.less assets/index.css", "coverage": "rc-test --coverage", "docs:build": "dumi build", - "docs:deploy": "gh-pages -d .doc", + "docs:deploy": "gh-pages -d docs-dist", + "gh-pages": "cross-env GH_PAGES=1 npm run docs:build && npm run docs:deploy", "lint": "eslint src/ --ext .tsx,.ts,.jsx,.js", - "now-build": "npm run build", "prepublishOnly": "npm run compile && rc-np", - "postpublish": "tnpm sync @rc-component/menu", + "prettier": "prettier --write --ignore-unknown .", "start": "dumi dev", "test": "rc-test", + "tsc": "tsc --noEmit", "prepare": "husky && dumi setup" }, "dependencies": { @@ -51,28 +53,29 @@ "clsx": "^2.1.1" }, "devDependencies": { - "@rc-component/father-plugin": "^2.0.2", - "@rc-component/np": "^1.0.3", - "@testing-library/jest-dom": "^6.1.5", - "@testing-library/react": "^16.0.0", - "@types/jest": "^30.0.0", - "@types/node": "^25.2.3", - "@types/react": "^18.2.14", + "@rc-component/father-plugin": "^2.2.0", + "@rc-component/np": "^1.0.4", + "@testing-library/jest-dom": "^6.9.1", + "@testing-library/react": "^15.0.7", + "@types/jest": "^29.5.14", + "@types/node": "^26.0.1", + "@types/react": "^18.3.31", "@types/react-dom": "^18.3.7", - "@types/warning": "^3.0.0", + "@types/warning": "^3.0.4", "@umijs/fabric": "^4.0.1", - "dumi": "^2.1.17", - "eslint": "^8.55.0", - "father": "^4.0.0", - "gh-pages": "^6.1.0", - "husky": "^9.1.6", - "less": "^4.1.3", - "lint-staged": "^16.2.4", - "prettier": "^3.3.3", - "rc-test": "^7.0.14", + "cross-env": "^10.1.0", + "dumi": "^2.4.35", + "eslint": "^8.57.1", + "father": "^4.6.23", + "gh-pages": "^6.3.0", + "husky": "^9.1.7", + "less": "^4.6.7", + "lint-staged": "^16.4.0", + "prettier": "^3.9.0", + "rc-test": "^7.1.3", "react": "^18.3.1", "react-dom": "^18.3.1", - "typescript": "^5.1.6" + "typescript": "^5.9.3" }, "peerDependencies": { "react": ">=16.9.0", @@ -80,5 +83,9 @@ }, "lint-staged": { "*": "prettier --write --ignore-unknown" - } + }, + "publishConfig": { + "access": "public" + }, + "types": "./es/index.d.ts" } diff --git a/tsconfig.json b/tsconfig.json index 0e7a3f4a..0f80b4f2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,9 @@ "@/*": ["src/*"], "@@/*": [".dumi/tmp/*"], "@rc-component/menu": ["src/index.ts"] - } - } + }, + "ignoreDeprecations": "5.0" + }, + "include": ["src", "docs", ".dumirc.ts", ".fatherrc.ts"], + "exclude": ["node_modules", "lib", "es", "dist", "docs-dist", ".dumi"] } diff --git a/vercel.json b/vercel.json new file mode 100644 index 00000000..5f9139ef --- /dev/null +++ b/vercel.json @@ -0,0 +1,6 @@ +{ + "framework": "umijs", + "installCommand": "npm install", + "buildCommand": "npm run build", + "outputDirectory": "docs-dist" +}