diff --git a/.dumirc.ts b/.dumirc.ts index 594242f..6c3f3e2 100644 --- a/.dumirc.ts +++ b/.dumirc.ts @@ -1,6 +1,9 @@ import { defineConfig } from 'dumi'; import path from 'path'; +const basePath = process.env.GH_PAGES ? '/notification/' : '/'; +const publicPath = basePath; + export default defineConfig({ alias: { '@rc-component/notification$': path.resolve('src'), @@ -11,4 +14,7 @@ export default defineConfig({ name: 'Notification', logo: 'https://avatars0.githubusercontent.com/u/9441414?s=200&v=4', }, + outputPath: 'docs-dist', + base: basePath, + publicPath, }); diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..758659a --- /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 6656d69..3b730ef 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,37 +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: react - versions: - - 17.0.1 - - dependency-name: typescript - versions: - - 4.1.3 - - 4.1.4 - - 4.1.5 - - 4.2.2 - - 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 89942c1..16da386 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 14 * * 2" + - cron: '4 14 * * 2' 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 f860ff1..0000000 --- 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 diff --git a/.github/workflows/react-component-ci.yml b/.github/workflows/react-component-ci.yml new file mode 100644 index 0000000..36dacae --- /dev/null +++ b/.github/workflows/react-component-ci.yml @@ -0,0 +1,9 @@ +name: ✅ test +on: [push, pull_request] +permissions: + contents: read +jobs: + test: + uses: react-component/rc-test/.github/workflows/test-utoo.yml@main + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/react-doctor.yml b/.github/workflows/react-doctor.yml new file mode 100644 index 0000000..097eb88 --- /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 0000000..4c2f17e --- /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 552c5b9..a82d792 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ build lib es coverage +docs-dist/ yarn.lock package-lock.json pnpm-lock.yaml @@ -43,5 +44,6 @@ pnpm-lock.yaml .dumi/tmp-production bun.lockb +.vercel .claude/skills/tmp-* diff --git a/.husky/pre-commit b/.husky/pre-commit index d24fdfc..2312dc5 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 0000000..c466d87 --- /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 0000000..bd0a1f7 --- /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 8d30871..90ceb21 100644 --- a/README.md +++ b/README.md @@ -1,194 +1,200 @@ -# @rc-component/notification - -React Notification UI Component - -[![NPM version][npm-image]][npm-url] [](https://github.com/umijs/dumi) [![build status][github-actions-image]][github-actions-url] [![Test coverage][coveralls-image]][coveralls-url] [![npm download][download-image]][download-url] [![bundle size][bundlephobia-image]][bundlephobia-url] - -[npm-image]: http://img.shields.io/npm/v/@rc-component/notification.svg?style=flat-square -[npm-url]: http://npmjs.org/package/@rc-component/notification -[github-actions-image]: https://github.com/react-component/notification/workflows/CI/badge.svg -[github-actions-url]: https://github.com/react-component/notification/actions -[coveralls-image]: https://img.shields.io/coveralls/react-component/notification.svg?style=flat-square -[coveralls-url]: https://coveralls.io/r/react-component/notification?branch=master -[download-image]: https://img.shields.io/npm/dm/@rc-component/notification.svg?style=flat-square -[download-url]: https://npmjs.org/package/@rc-component/notification -[bundlephobia-url]: https://bundlephobia.com/result?p=@rc-component/notification -[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/@rc-component/notification +
Part of the Ant Design ecosystem.
🔔 Hook-based React notification primitives for stacked, animated, and accessible notices.
+ + +English | 简体中文
+ +## Highlights + +- `useNotification` hook returning an API and React holder element. +- Top, bottom, left, and right placements with max-count limiting. +- Closable notices, duration timers, progress display, hover pause, and stacked layout. +- Custom motion, semantic `classNames` / `styles`, progress component override, and provider-level classes. +- TypeScript definitions for notification config, API, list config, and progress props. +- Used by Ant Design as the shared notification foundation. ## Install -[](https://npmjs.org/package/@rc-component/notification) +```bash +npm install @rc-component/notification +``` ## Usage -```js -import Notification from '@rc-component/notification'; +```tsx | pure +import { useNotification } from '@rc-component/notification'; + +export default () => { + const [api, holder] = useNotification(); + + return ( + <> + {holder} + + > + ); +}; +``` -Notification.newInstance({}, (notification) => { - notification.notice({ - content: 'content', +```tsx | pure +import { NotificationProvider, useNotification } from '@rc-component/notification'; + +const Demo = () => { + const [api, holder] = useNotification({ + placement: 'topRight', + maxCount: 3, + showProgress: true, + pauseOnHover: true, + stack: true, }); -}); -``` -## Compatibility + return ( + <> + {holder} + + > + ); +}; + +export default () => ( +| name | -type | -default | -description | -
|---|---|---|---|
| prefixCls | -String | -- | prefix class name for notification container | -
| style | -Object | -{'top': 65, left: '50%'} | -additional style for notification container. | -
| getContainer | -getContainer(): HTMLElement | -- | function returning html node which will act as notification container | -
| maxCount | -number | -- | max notices show, drop first notice if exceed limit | -
| name | -type | -default | -description | -
|---|---|---|---|
| content | -React.Element | -- | content of notice | -
| key | -String | -- | id of this notice | -
| closable | -Boolean | { closeIcon: ReactNode, onClose: VoidFunction } | -- | whether show close button | -
| onClose | -Function | -- | called when notice close | -
| duration | -number | false | -4.5 | -The delay for automatic closing in seconds. Set to 0 or false to not close automatically. | -
| showProgress | -boolean | -false | -show with progress bar for auto-closing notification | -
| pauseOnHover | -boolean | -true | -keep the timer running or not on hover | -
| style | -Object | -{ right: '50%' } | -additional style for single notice node. | -
| closeIcon | -ReactNode | -- | specific the close icon. | -
| props | -Object | -- | An object that can contain data-*, aria-*, or role props, to be put on the notification div. This currently only allows data-testid instead of data-* in TypeScript. See https://github.com/microsoft/TypeScript/issues/28960. |
-
Ant Design 生态的一部分。
🔔 React 通知组件,支持堆叠、位置、动画和全局调用。
+ + +English | 简体中文
+ +## 特性 + +- `useNotification` 钩子返回 API 和 React 持有者元素。 +- 具有最大数量限制的顶部、底部、左侧和右侧展示位置。 +- 可关闭通知、持续计时器、进度显示、悬停暂停和堆叠布局。 +- 支持动画自定义、语义化 `classNames` / `styles` 、细节组件覆盖和 Provider 级类。 +- 通知配置、API、列表配置和进度属性的 TypeScript 定义。 +- 被 Ant Design 用作共享的 notification 基础能力。 + +## 安装 + +```bash +npm install @rc-component/notification +``` + +## 使用 + +```tsx | pure +import { useNotification } from '@rc-component/notification'; + +export default () => { + const [api, holder] = useNotification(); + + return ( + <> + {holder} + + > + ); +}; +``` + +```tsx | pure +import { NotificationProvider, useNotification } from '@rc-component/notification'; + +const Demo = () => { + const [api, holder] = useNotification({ + placement: 'topRight', + maxCount: 3, + showProgress: true, + pauseOnHover: true, + stack: true, + }); + + return ( + <> + {holder} + + > + ); +}; + +export default () => ( +1
, + closable: false, + duration: 0, + }); + }); + + expect(document.querySelector('.test')).toBeTruthy(); + expect(document.querySelector('.rc-notification-notice-close')).toBeFalsy(); + }); + it('works with multi instance', () => { const { instance } = renderDemo(); @@ -637,7 +656,7 @@ describe('Notification.Basic', () => { it('should style work', () => { const { instance } = renderDemo({ style: () => ({ - content: 'little', + content: '"little"', }), }); @@ -645,9 +664,7 @@ describe('Notification.Basic', () => { instance.open({}); }); - expect(document.querySelector('.rc-notification')).toHaveStyle({ - content: 'little', - }); + expectContentStyle('.rc-notification', 'little'); }); it('should open style and className work', () => { @@ -656,15 +673,13 @@ describe('Notification.Basic', () => { act(() => { instance.open({ style: { - content: 'little', + content: '"little"', }, className: 'bamboo', }); }); - expect(document.querySelector('.rc-notification-notice')).toHaveStyle({ - content: 'little', - }); + expectContentStyle('.rc-notification-notice', 'little'); expect(document.querySelector('.rc-notification-notice')).toHaveClass('bamboo'); }); @@ -706,7 +721,7 @@ describe('Notification.Basic', () => { description: 'little', styles: { section: { - content: 'light', + content: '"light"', }, }, classNames: { @@ -715,9 +730,7 @@ describe('Notification.Basic', () => { }); }); - expect(document.querySelector('.rc-notification-notice-section')).toHaveStyle({ - content: 'light', - }); + expectContentStyle('.rc-notification-notice-section', 'light'); expect(document.querySelector('.rc-notification-notice-section')).toHaveClass('section-class'); }); @@ -729,7 +742,7 @@ describe('Notification.Basic', () => { icon: , styles: { wrapper: { - content: 'little', + content: '"little"', }, }, classNames: { @@ -738,9 +751,7 @@ describe('Notification.Basic', () => { }); }); - expect(document.querySelector('.rc-notification-notice-wrapper')).toHaveStyle({ - content: 'little', - }); + expectContentStyle('.rc-notification-notice-wrapper', 'little'); expect(document.querySelector('.rc-notification-notice-wrapper')).toHaveClass('bamboo'); }); @@ -777,16 +788,16 @@ describe('Notification.Basic', () => { }, styles: { title: { - content: 'global-title', + content: '"global-title"', }, description: { - content: 'global-description', + content: '"global-description"', }, actions: { - content: 'global-actions', + content: '"global-actions"', }, icon: { - content: 'global-icon', + content: '"global-icon"', }, }, }); @@ -824,24 +835,24 @@ describe('Notification.Basic', () => { 'global-title', 'notice-title', ); + expectContentStyle('.rc-notification-notice-title', 'global-title'); expect(document.querySelector('.rc-notification-notice-title')).toHaveStyle({ - content: 'global-title', marginTop: '1px', }); expect(document.querySelector('.rc-notification-notice-description')).toHaveClass( 'global-description', 'notice-description', ); + expectContentStyle('.rc-notification-notice-description', 'global-description'); expect(document.querySelector('.rc-notification-notice-description')).toHaveStyle({ - content: 'global-description', marginRight: '2px', }); expect(document.querySelector('.rc-notification-notice-actions')).toHaveClass( 'global-actions', 'notice-actions', ); + expectContentStyle('.rc-notification-notice-actions', 'global-actions'); expect(document.querySelector('.rc-notification-notice-actions')).toHaveStyle({ - content: 'global-actions', marginBottom: '3px', }); expect(document.querySelector('.actions')).toBeFalsy(); @@ -849,8 +860,8 @@ describe('Notification.Basic', () => { 'global-icon', 'notice-icon', ); + expectContentStyle('.rc-notification-notice-icon', 'global-icon'); expect(document.querySelector('.rc-notification-notice-icon')).toHaveStyle({ - content: 'global-icon', marginLeft: '4px', }); }); @@ -875,10 +886,10 @@ describe('Notification.Basic', () => { }, styles: { list: { - content: 'root-list', + content: '"root-list"', }, listContent: { - content: 'little', + content: '"little"', }, }, }); @@ -887,13 +898,9 @@ describe('Notification.Basic', () => { instance.open({}); }); - expect(document.querySelector('.rc-notification-list')).toHaveStyle({ - content: 'root-list', - }); + expectContentStyle('.rc-notification-list', 'root-list'); expect(document.querySelector('.rc-notification-list')).toHaveClass('root-list'); - expect(document.querySelector('.rc-notification-list-content')).toHaveStyle({ - content: 'little', - }); + expectContentStyle('.rc-notification-list-content', 'little'); expect(document.querySelector('.rc-notification-list-content')).toHaveClass('bamboo'); }); diff --git a/tsconfig.json b/tsconfig.json index 19a30b9..5c36c9d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,10 +8,39 @@ "skipLibCheck": true, "esModuleInterop": true, "paths": { - "@/*": ["src/*"], - "@@/*": [".dumi/tmp/*"], - "rc-notification": ["src/index.tsx"] + "@/*": [ + "src/*" + ], + "@@/*": [ + ".dumi/tmp/*" + ], + "@rc-component/notification": [ + "src/index.ts" + ], + "rc-notification": [ + "src/index.ts" + ] }, - "types": ["vitest/globals"] - } + "types": [ + "vitest/globals" + ], + "ignoreDeprecations": "5.0" + }, + "include": [ + "src", + "docs", + "tests", + ".dumirc.ts", + ".fatherrc.ts", + "vitest.config.ts", + "vitest-setup.ts" + ], + "exclude": [ + "node_modules", + "lib", + "es", + "dist", + "docs-dist", + ".dumi" + ] } diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..5f9139e --- /dev/null +++ b/vercel.json @@ -0,0 +1,6 @@ +{ + "framework": "umijs", + "installCommand": "npm install", + "buildCommand": "npm run build", + "outputDirectory": "docs-dist" +}