Skip to content

Commit

Permalink
Merge pull request #13 from make3waves/feature/bc
Browse files Browse the repository at this point in the history
toast component
  • Loading branch information
shaobeichen committed Nov 18, 2022
2 parents 74ffe98 + a7f770f commit a5cfdc4
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v3
with:
Expand Down
20 changes: 19 additions & 1 deletion .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"branches": [
{
"name": "main"
},
{
"name": "next",
"channel": "next",
"prerelease": true
}
],
"plugins": [
Expand All @@ -14,11 +19,24 @@
{
"assets": [
"package.json",
"package-lock.json",
"CHANGELOG.md"
],
"message": "release: v${nextRelease.version} [skip ci] \n\n${nextRelease.notes}"
}
],
"@semantic-release/github"
"@semantic-release/github",
[
"@saithodev/semantic-release-backmerge",
{
"branches": [
{
"from": "main",
"to": "next"
}
],
"clearWorkspace": true
}
]
]
}
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# makaka

轻量级 React 组件库
<h1 align="center">makaka</h1>

<p align="center">
轻量级 React 组件库
</p>

<p align="center">
<a href="https://www.npmjs.com/package/makaka">
<img src="https://img.shields.io/npm/v/makaka" />
</a>
<a href="https://www.npmjs.com/package/makaka">
<img src="https://img.shields.io/npm/dt/makaka.svg">
</a>
<a href="https://github.com/make3waves/makaka/actions/workflows/release.yml">
<img src="https://github.com/make3waves/makaka/actions/workflows/release.yml/badge.svg" />
</a>
</p>

### 使用

Expand Down Expand Up @@ -48,6 +62,14 @@ npm run build

发布 NPM 包通过 Github Actions 自动发布,合并到 main 分支即会自动发包

### TODOLIST

- [] 样式前缀名
- [] 按需加载
- [] 组件支持SSR
- [] 浏览器引入 [案例](https://ant.design/docs/react/introduce-cn#%E6%B5%8F%E8%A7%88%E5%99%A8%E5%BC%95%E5%85%A5)
- [] 兼容环境 [案例](https://ant.design/docs/react/introduce-cn#%E5%85%BC%E5%AE%B9%E7%8E%AF%E5%A2%83)

### 参考

[nutui-react](https://nutui.jd.com/react/#/zh-CN/guide/start-react)
5 changes: 2 additions & 3 deletions examples/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import ReactDOM from 'react-dom'
import ReactDOM from 'react-dom/client'
// 这里是重点!!!
// App为库的入口,模拟外部引入
import App from '../src/App'
Expand All @@ -11,7 +11,7 @@ const containerStyle: React.CSSProperties = {
transform: 'translate(-50%, -50%)',
}

ReactDOM.render(
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<div style={containerStyle}>
<a href="./examples/test.html">跳转到线上包预览页面</a>
Expand All @@ -21,5 +21,4 @@ ReactDOM.render(
</div>
</div>
</React.StrictMode>,
document.getElementById('root'),
)
4 changes: 2 additions & 2 deletions examples/test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { createRoot } from 'react-dom/client'
import ReactDOM from 'react-dom/client'
import { Button } from 'makaka'
import 'makaka/lib/style.css'

Expand Down Expand Up @@ -31,7 +31,7 @@ const App: React.FC = () => {
)
}

createRoot(document.getElementById('root') as HTMLElement).render(
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>,
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
},
"scripts": {
"dev": "vite",
"build": "npm run clean && vite build && npm run build-css && npm run copy-readme",
"build": "npm run test && npm run clean && vite build && npm run build:css && npm run copy-readme",
"preview": "vite preview",
"build-css": "sass ./src/styles/index.scss ./lib/index.css",
"build:css": "sass ./src/styles/index.scss ./lib/index.css",
"copy-readme": "cp ./README.md ./lib/README.md",
"clean": "rimraf ./lib",
"test": "react-scripts test --watchAll=false",
"test:coverage": "react-scripts test --watchAll=false -- --coverage",
"eject": "react-scripts eject",
"release": "npm run semantic-release",
"release:beta": "standard-version --release-as major --prerelease beta",
"release:major": "standard-version --release-as major",
Expand Down Expand Up @@ -51,7 +54,7 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"makaka": "^0.1.0"
"makaka": "^0.3.0"
},
"peerDependencies": {
"react": "^18.2.0",
Expand All @@ -60,6 +63,7 @@
"devDependencies": {
"@babel/core": "^7.19.6",
"@rollup/plugin-typescript": "^9.0.2",
"@saithodev/semantic-release-backmerge": "^2.1.2",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
Expand Down Expand Up @@ -96,4 +100,4 @@
"typescript": "^4.6.4",
"vite": "^3.2.0"
}
}
}
3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import Button, { ButtonType, ButtonSize } from './components/Button/button'
import Toast from './components/Toast/toast'
import './styles/index.scss'

const App: React.FC = () => {
Expand All @@ -17,4 +18,6 @@ const App: React.FC = () => {
)
}

Toast('你好,我是一个命令式组件,我通过调用命令生成一个DOM节点。')

export default App
14 changes: 14 additions & 0 deletions src/components/Toast/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.toast {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: rgba($color: black, $alpha: 0.7);
color: white;
font-size: 16px;
text-align: center;
max-width: 400px;
z-index: 9999;
padding: 10px 20px;
border-radius: 14px;
}
Empty file.
19 changes: 19 additions & 0 deletions src/components/Toast/toast.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Toast, { ToastProps } from './toast'

const testProps: ToastProps = {
message: '你好,我是命令式组件 Toast 。',
}

describe('test toast components', () => {
it('should render toast', () => {
Toast(testProps)
setTimeout(() => {
const element = document.querySelector('.toast')
expect(element?.innerHTML).toBe(testProps.message)
expect(element).toHaveClass('toast')
setTimeout(() => {
expect(element).not.toBeTruthy()
}, 4000)
}, 0)
})
})
45 changes: 45 additions & 0 deletions src/components/Toast/toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react'
import { createRoot } from 'react-dom/client'

interface BaseToastProps {
message?: string | React.ReactNode
duration?: number
style?: React.CSSProperties
}

type AnchorToastProps = BaseToastProps & React.AnchorHTMLAttributes<HTMLElement>
export type ToastProps = Partial<AnchorToastProps>

const Toast = (props: string | ToastProps) => {
const defaultDuration = 3000

let message: BaseToastProps['message']
let duration: BaseToastProps['duration'] = defaultDuration

if (typeof props === 'string') {
message = props
} else {
message = props.message
duration = props.duration || defaultDuration
}
const content = <div className="toast">{message}</div>

const container = document.createElement('div')
document.body.appendChild(container)

const root = createRoot(container)

const init = () => {
const id = setTimeout(() => {
clearTimeout(id)
root.unmount()
document.body.removeChild(container)
}, duration)
}

init()

return root.render(content)
}

export default Toast
3 changes: 3 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { default as Button } from './components/Button/button'
export type { ButtonProps } from './components/Button/button'

export { default as Toast } from './components/Toast/toast'
export type { ToastProps } from './components/Toast/toast'
4 changes: 2 additions & 2 deletions src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// button
@import '../components/Button/style';

// alert
// @import '../components/Alert/style';
// toast
@import '../components/Toast/style';

// menu
// @import '../components/Menu/style';
Expand Down

0 comments on commit a5cfdc4

Please sign in to comment.