diff --git a/.dumirc.ts b/.dumirc.ts index 2d8caaab..d5d1c9f8 100644 --- a/.dumirc.ts +++ b/.dumirc.ts @@ -1,21 +1,23 @@ -// more config: https://d.umijs.org/config import { defineConfig } from 'dumi'; import path from 'path'; -const basePath = process.env.GITHUB_ACTIONS ? '/util/' : '/'; -const publicPath = process.env.GITHUB_ACTIONS ? '/util/' : '/'; +const basePath = process.env.GH_PAGES ? '/util/' : '/'; +const publicPath = basePath; export default defineConfig({ + alias: { + '@rc-component/util$': path.resolve(__dirname, 'src'), + '@rc-component/util/es': path.resolve(__dirname, 'src'), + 'rc-util$': path.resolve(__dirname, 'src'), + 'rc-util/es': path.resolve(__dirname, 'src'), + }, + mfsu: false, favicons: ['https://avatars0.githubusercontent.com/u/9441414?s=200&v=4'], themeConfig: { name: 'Util', logo: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4', }, - outputPath: '.doc', - exportStatic: {}, + outputPath: 'docs-dist', base: basePath, publicPath, - alias: { - 'rc-util/es': path.resolve(__dirname, 'src'), - }, }); diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..758659af --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: ant-design +open_collective: ant-design diff --git a/.github/dependabot.yml b/.github/dependabot.yml index f5c5b6e8..3b730ef9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,48 +1,19 @@ version: 2 updates: -- package-ecosystem: npm - directory: "/" - schedule: - interval: daily - time: "21:00" - open-pull-requests-limit: 10 - ignore: - - dependency-name: eslint - versions: - - 7.18.0 - - 7.19.0 - - 7.20.0 - - 7.21.0 - - 7.22.0 - - 7.23.0 - - 7.24.0 - - dependency-name: "@types/jest" - versions: - - 26.0.20 - - 26.0.21 - - 26.0.22 - - dependency-name: react - versions: - - 17.0.1 - - dependency-name: "@types/react" - versions: - - 17.0.0 - - 17.0.1 - - 17.0.2 - - 17.0.3 - - dependency-name: "@types/react-dom" - versions: - - 17.0.0 - - 17.0.1 - - 17.0.2 - - dependency-name: react-dom - versions: - - 17.0.1 - - dependency-name: np - versions: - - 7.2.0 - - 7.3.0 - - 7.4.0 - - dependency-name: react-is - versions: - - 17.0.1 + - 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 ecfbc108..4ddf3594 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: "4 6 * * 0" + - cron: '40 12 * * 0' 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/react-component-ci.yml b/.github/workflows/react-component-ci.yml index 21c24587..d91f4b47 100644 --- a/.github/workflows/react-component-ci.yml +++ b/.github/workflows/react-component-ci.yml @@ -1,6 +1,15 @@ name: ✅ test -on: [push, pull_request] + +on: + push: + branches: [master] + pull_request: + branches: [master] + +permissions: + contents: read + jobs: test: - uses: react-component/rc-test/.github/workflows/test-npm.yml@main + 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..56430b6e --- /dev/null +++ b/.github/workflows/react-doctor.yml @@ -0,0 +1,24 @@ +name: React Doctor + +on: + pull_request: + branches: [master] + +permissions: + contents: read + pull-requests: 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: + persist-credentials: false + - uses: millionco/react-doctor@0b4f4f4bd248a154e64eb508a48347f71154b3f3 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/site-deploy.yml b/.github/workflows/site-deploy.yml deleted file mode 100644 index 8d4aaff4..00000000 --- a/.github/workflows/site-deploy.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Deploy website -on: - push: - tags: - - '*' - workflow_dispatch: - -permissions: - contents: write - -jobs: - build-and-deploy: - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v3 - - - name: setup node - uses: actions/setup-node@v1 - with: - node-version: 20 - - - name: create package-lock.json - run: npm i --package-lock-only --ignore-scripts - - - name: Install dependencies - run: npm ci - - - name: build Docs - run: npm run build - - - name: Deploy to GitHub Pages - uses: peaceiris/actions-gh-pages@v3 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./.doc - force_orphan: true - user_name: 'github-actions[bot]' - user_email: 'github-actions[bot]@users.noreply.github.com' diff --git a/.github/workflows/surge-preview.yml b/.github/workflows/surge-preview.yml new file mode 100644 index 00000000..0a721ebf --- /dev/null +++ b/.github/workflows/surge-preview.yml @@ -0,0 +1,48 @@ +name: Surge Preview + +on: + pull_request: + branches: [master] + +permissions: + contents: read + pull-requests: write + statuses: write + +concurrency: + group: surge-preview-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + preview: + runs-on: ubuntu-latest + 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: Install dependencies + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + run: npm install --legacy-peer-deps --ignore-scripts --no-audit --loglevel=warn + - name: Build preview + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + run: npm run build + - uses: afc163/surge-preview@bf90a5a86111f6311ca42f0a5a0f80fb0fb03cec + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + NPM_CONFIG_LEGACY_PEER_DEPS: 'true' + with: + dist: docs-dist + failOnError: false + github_token: ${{ secrets.GITHUB_TOKEN }} + surge_token: ${{ env.SURGE_TOKEN }} diff --git a/.gitignore b/.gitignore index 97a23476..32dd1191 100644 --- a/.gitignore +++ b/.gitignore @@ -30,13 +30,14 @@ pnpm-lock.yaml yarn.lock .doc bun.lockb - # umi .umi .umi-production .umi-test .env.local - .dumi/tmp .dumi/tmp-test .dumi/tmp-production +es +docs-dist +.vercel diff --git a/.husky/pre-commit b/.husky/pre-commit index d24fdfc6..2312dc58 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - 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/README.md b/README.md index 7b8ae351..ee0e3e66 100644 --- a/README.md +++ b/README.md @@ -1,303 +1,93 @@ -# rc-util +
Part of the Ant Design ecosystem.
🛠️ Shared React utilities for rc-component packages.
-Common Utils For React Component. + +English | 简体中文
-[npm-image]: http://img.shields.io/npm/v/rc-util.svg?style=flat-square -[npm-url]: http://npmjs.org/package/rc-util -[travis-image]: https://img.shields.io/travis/react-component/util/master?style=flat-square -[travis-url]: https://travis-ci.com/react-component/util -[github-actions-image]: https://github.com/react-component/util/actions/workflows/react-component-ci.yml/badge.svg -[github-actions-url]: https://github.com/react-component/util/actions/workflows/react-component-ci.yml -[codecov-image]: https://img.shields.io/codecov/c/github/react-component/util/master.svg?style=flat-square -[codecov-url]: https://app.codecov.io/gh/react-component/util -[david-url]: https://david-dm.org/react-component/util -[david-image]: https://david-dm.org/react-component/util/status.svg?style=flat-square -[david-dev-url]: https://david-dm.org/react-component/util?type=dev -[david-dev-image]: https://david-dm.org/react-component/util/dev-status.svg?style=flat-square -[download-image]: https://img.shields.io/npm/dm/rc-util.svg?style=flat-square -[download-url]: https://npmjs.org/package/rc-util -[bundlephobia-url]: https://bundlephobia.com/package/rc-util -[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-util -[dumi-url]: https://github.com/umijs/dumi -[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square -## Install - -[](https://npmjs.org/package/rc-util) - -## API - -### createChainedFunction - -> (...functions): Function - -Create a function which will call all the functions with it's arguments from left to right. - -```jsx|pure -import createChainedFunction from 'rc-util/lib/createChainedFunction'; -``` - -### deprecated - -> (prop: string, instead: string, component: string): void - -Log an error message to warn developers that `prop` is deprecated. - -```jsx|pure -import deprecated from 'rc-util/lib/deprecated'; -``` - -### getContainerRenderMixin - -> (config: Object): Object - -To generate a mixin which will render specific component into specific container automatically. - -```jsx|pure -import getContainerRenderMixin from 'rc-util/lib/getContainerRenderMixin'; -``` - -Fields in `config` and their meanings. - -| Field | Type | Description | Default | -| ------------- | ---------------------------- | -------------------------------------------------------------------------- | ------- | -| autoMount | boolean | Whether to render component into container automatically | true | -| autoDestroy | boolean | Whether to remove container automatically while the component is unmounted | true | -| isVisible | (instance): boolean | A function to get current visibility of the component | - | -| isForceRender | (instance): boolean | A function to determine whether to render popup even it's not visible | - | -| getComponent | (instance, extra): ReactNode | A function to get the component which will be rendered into container | - | -| getContainer | (instance): HTMLElement | A function to get the container | | - -### Portal - -Render children to the specific container; - -```jsx|pure -import Portal from 'rc-util/lib/Portal'; -``` - -Props: - -| Prop | Type | Description | Default | -| ------------ | --------------- | ------------------------------- | ------- | -| children | ReactChildren | Content render to the container | - | -| getContainer | (): HTMLElement | A function to get the container | - | - -### getScrollBarSize - -> (fresh?: boolean): number - -Get the width of scrollbar. - -```jsx|pure -import getScrollBarSize from 'rc-util/lib/getScrollBarSize'; -``` - -### guid - -> (): string - -To generate a global unique id across current application. - -```jsx|pure -import guid from 'rc-util/lib/guid'; -``` - -### pickAttrs - -> (props: Object): Object - -Pick valid HTML attributes and events from props. - -```jsx|pure -import pickAttrs from 'rc-util/lib/pickAttrs'; -``` - -### warn - -> (msg: string): void - -A shallow wrapper of `console.warn`. - -```jsx|pure -import warn from 'rc-util/lib/warn'; -``` - -### warning - -> (valid: boolean, msg: string): void - -A shallow wrapper of [warning](https://github.com/BerkeleyTrue/warning), but only warning once for the same message. +## Highlights -```jsx|pure -import warning, { noteOnce } from 'rc-util/lib/warning'; +- Built for React and maintained by the rc-component team. +- Used by Ant Design and other React component libraries. +- Ships TypeScript declarations with both ES module and CommonJS outputs. +- Keeps examples, tests, and preview builds aligned with the package source. -warning(false, '[antd Component] test hello world'); +> This package was historically published as `rc-util`. Prefer the scoped package for new code. -// Low level note -noteOnce(false, '[antd Component] test hello world'); -``` - -### Children - -A collection of functions to operate React elements' children. - -#### Children/mapSelf - -> (children): children - -Return a shallow copy of children. - -```jsx|pure -import mapSelf from 'rc-util/lib/Children/mapSelf'; -``` - -#### Children/toArray - -> (children: ReactNode[]): ReactNode[] - -Convert children into an array. - -```jsx|pure -import toArray from 'rc-util/lib/Children/toArray'; -``` - -### Dom - -A collection of functions to operate DOM elements. - -#### Dom/addEventlistener - -> (target: ReactNode, eventType: string, listener: Function): { remove: Function } - -A shallow wrapper of [add-dom-event-listener](https://github.com/yiminghe/add-dom-event-listener). - -```jsx|pure -import addEventlistener from 'rc-util/lib/Dom/addEventlistener'; -``` - -#### Dom/canUseDom - -> (): boolean - -Check if DOM is available. - -```jsx|pure -import canUseDom from 'rc-util/lib/Dom/canUseDom'; -``` - -#### Dom/class - -A collection of functions to operate DOM nodes' class name. - -- `hasClass(node: HTMLElement, className: string): boolean` -- `addClass(node: HTMLElement, className: string): void` -- `removeClass(node: HTMLElement, className: string): void` +## Install -```jsx|pure -import cssClass from 'rc-util/lib/Dom/class; +```bash +npm install @rc-component/util ``` -#### Dom/contains +## Usage -> (root: HTMLElement, node: HTMLElement): boolean - -Check if node is equal to root or in the subtree of root. - -```jsx|pure -import contains from 'rc-util/lib/Dom/contains'; +```tsx | pure +import { + Portal, + raf, + useEvent, + useLayoutEffect, + warning, +} from '@rc-component/util'; ``` -#### Dom/css - -A collection of functions to get or set css styles. +## Examples -- `get(node: HTMLElement, name?: string): any` -- `set(node: HTMLElement, name?: string, value: any) | set(node, object)` -- `getOuterWidth(el: HTMLElement): number` -- `getOuterHeight(el: HTMLElement): number` -- `getDocSize(): { width: number, height: number }` -- `getClientSize(): { width: number, height: number }` -- `getScroll(): { scrollLeft: number, scrollTop: number }` -- `getOffset(node: HTMLElement): { left: number, top: number }` +Run the local dumi site: -```jsx|pure -import css from 'rc-util/lib/Dom/css'; +```bash +npm install +npm start ``` -#### Dom/focus +Then open `http://localhost:8000`. -A collection of functions to operate focus status of DOM node. - -- `saveLastFocusNode(): void` -- `clearLastFocusNode(): void` -- `backLastFocusNode(): void` -- `getFocusNodeList(node: HTMLElement): HTMLElement[]` get a list of focusable nodes from the subtree of node. -- `limitTabRange(node: HTMLElement, e: Event): void` +## API -```jsx|pure -import focus from 'rc-util/lib/Dom/focus'; -``` +The package exposes small, focused helpers used across rc-component and Ant Design packages. Import the public entry when possible, and use subpath imports for specialized DOM, React, or test helpers. -#### Dom/support +| Area | Examples | +| --------------- | ---------------------------------------------------------------------------------- | +| React hooks | `useEvent`, `useLayoutEffect`, `useMergedState`, `useState` helpers | +| React utilities | `Children/toArray`, `composeRef`, `pickAttrs`, `Portal` | +| DOM utilities | `Dom/canUseDom`, `Dom/contains`, `Dom/dynamicCSS`, `Dom/focus`, `getScrollBarSize` | +| Async helpers | `raf`, `raf.cancel` | +| Warnings | `warning`, `noteOnce`, `resetWarned` | +| Tests | `test/domHook` for DOM prototype spies | -> { animation: boolean | Object, transition: boolean | Object } +Examples in `docs/examples` cover the commonly used subpath helpers. -A flag to tell whether current environment supports `animationend` or `transitionend`. +## Development -```jsx|pure -import support from 'rc-util/lib/Dom/support'; +```bash +npm install +npm start +npm test +npm run build ``` -### KeyCode +The dumi site runs at `http://localhost:8000` by default. -> Enum +## Release -Enum of KeyCode, please check the [definition](https://github.com/react-component/util/blob/master/src/KeyCode.ts) of it. - -```jsx|pure -import KeyCode from 'rc-util/lib/KeyCode'; +```bash +npm run prepublishOnly ``` -#### KeyCode.isTextModifyingKeyEvent - -> (e: Event): boolean - -Whether text and modified key is entered at the same time. - -#### KeyCode.isCharacterKey - -> (keyCode: KeyCode): boolean - -Whether character is entered. - -### ScrollLocker - -> ScrollLocker<{lock: (options: {container: HTMLElement}) => void, unLock: () => void}> - -improve shake when page scroll bar hidden. - -`ScrollLocker` change body style, and add a class `ant-scrolling-effect` when called, so if you page look abnormal, please check this; - -```js -import ScrollLocker from 'rc-util/lib/Dom/scrollLocker'; - -const scrollLocker = new ScrollLocker(); - -// lock -scrollLocker.lock() - -// unLock -scrollLocker.unLock() -``` +The release flow is handled by `@rc-component/np` through the `rc-np` command after the package build. ## License -[MIT](/LICENSE) +@rc-component/util 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..0b3e6dbc --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,93 @@ + Ant Design 生态的一部分。
🛠️ rc-component 共享工具集合,包含 DOM、React 和测试辅助能力。
+ + +English | 简体中文
+ + +## 特性 + +- 面向 React 构建,并由 rc-component 团队维护。 +- 被 Ant Design 使用和其他 React 组件库使用。 +- 提供 TypeScript 类型声明,同时输出 ES module 和 CommonJS 产物。 +- 示例、测试和预览构建与包源码保持一致。 + +> 该包历史上曾以 `rc-util` 发布。新代码推荐使用带 scope 的包名。 + +## 安装 + +```bash +npm install @rc-component/util +``` + +## 使用 + +```tsx | pure +import { + Portal, + raf, + useEvent, + useLayoutEffect, + warning, +} from '@rc-component/util'; +``` + +## 示例 + +运行本地 dumi 站点: + +```bash +npm install +npm start +``` + +然后打开 `http://localhost:8000`。 + +## API + +该包提供在 rc-component 和 Ant Design 包中复用的小型聚焦工具。优先导入公开入口;针对 DOM、React 或测试辅助能力,可使用子路径导入。 + +| 范围 | 示例 | +| --------------- | ---------------------------------------------------------------------------------- | +| React hooks | `useEvent`, `useLayoutEffect`, `useMergedState`, `useState` 辅助方法 | +| React 工具 | `Children/toArray`, `composeRef`, `pickAttrs`, `Portal` | +| DOM 工具 | `Dom/canUseDom`, `Dom/contains`, `Dom/dynamicCSS`, `Dom/focus`, `getScrollBarSize` | +| 异步辅助方法 | `raf`, `raf.cancel` | +| 警告工具 | `warning`, `noteOnce`, `resetWarned` | +| 测试工具 | `test/domHook` 用于 DOM 原型 spy | + +`docs/examples` 中的示例覆盖了常用的子路径辅助工具。 + +## 本地开发 + +```bash +npm install +npm start +npm test +npm run build +``` + +dumi 站点默认运行在 `http://localhost:8000`。 + +## 发布 + +```bash +npm run prepublishOnly +``` + +包构建完成后,发布流程由 `@rc-component/np` 通过 `rc-np` 命令处理。 + +## 许可证 + +@rc-component/util 基于 [MIT](./LICENSE) 许可证发布。 diff --git a/docs/examples/dynaymicCSS.tsx b/docs/examples/dynaymicCSS.tsx index d77f14ee..3b6d3d43 100644 --- a/docs/examples/dynaymicCSS.tsx +++ b/docs/examples/dynaymicCSS.tsx @@ -1,5 +1,5 @@ -import type { Prepend } from 'rc-util/es/Dom/dynamicCSS'; -import { removeCSS, updateCSS } from 'rc-util/es/Dom/dynamicCSS'; +import type { Prepend } from '@rc-component/util/es/Dom/dynamicCSS'; +import { removeCSS, updateCSS } from '@rc-component/util/es/Dom/dynamicCSS'; import React from 'react'; function injectStyle(id: number, prepend?: Prepend, priority?: number) { diff --git a/docs/examples/getScrollBarSize.tsx b/docs/examples/getScrollBarSize.tsx index b6988f6f..5abedc5e 100644 --- a/docs/examples/getScrollBarSize.tsx +++ b/docs/examples/getScrollBarSize.tsx @@ -1,6 +1,6 @@ import getScrollBarSize, { getTargetScrollBarSize, -} from 'rc-util/es/getScrollBarSize'; +} from '@rc-component/util/es/getScrollBarSize'; import React from 'react'; const cssText = ` diff --git a/docs/examples/portal.tsx b/docs/examples/portal.tsx index 50b30c3a..9f5b2b34 100644 --- a/docs/examples/portal.tsx +++ b/docs/examples/portal.tsx @@ -1,24 +1,29 @@ import React from 'react'; -import PortalWrapper from 'rc-util/es/PortalWrapper'; +import { Portal } from '@rc-component/util'; const Demo: React.FC = () => { const divRef = React.useRefPress F12 to open the console (Click Clear Console)
+1
2