Skip to content

Commit

Permalink
feat(toolbar): react support
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Jul 2, 2023
1 parent f70010b commit 395b558
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/luna.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const build = require('../lib/build')
const format = wrap(async function (component) {
await runScript('lsla', [
'prettier',
`src/${component}/**/*.{ts,js,html,json,css,scss}`,
`src/${component}/**/*.{ts,tsx,js,html,json,css,scss}`,
'--write',
])
})
Expand Down
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@
"dependencies": []
},
"toolbar": {
"react": true,
"version": "0.3.1",
"style": true,
"icon": false,
"test": true,
"install": false,
"react": false,
"dependencies": []
},
"video-player": {
Expand Down
10 changes: 6 additions & 4 deletions src/modal/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ const LunaModal: FC<PropsWithChildren<IModalProps>> = (props) => {
}
}, [props.width])

return <div ref={modalRef}>
{createPortal(<>{props.children}</>, content.current)}
</div>
return (
<div ref={modalRef}>
{createPortal(<>{props.children}</>, content.current)}
</div>
)
}

export default LunaModal
export default LunaModal
5 changes: 4 additions & 1 deletion src/toolbar/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "toolbar",
"version": "0.3.1",
"description": "Application toolbar"
"description": "Application toolbar",
"luna": {
"react": true
}
}
100 changes: 100 additions & 0 deletions src/toolbar/react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
PropsWithChildren,
FC,
useRef,
useEffect,
cloneElement,
Children,
ReactElement,
useState,
} from 'react'
import types from 'licia/types'
import Toolbar from './index'

interface IToolbarProps {}

const LunaToolbar: FC<PropsWithChildren<IToolbarProps>> = (props) => {
const toolbarRef = useRef<HTMLDivElement>(null)
const toolbar = useRef<Toolbar>()
const [_, setForceUpdateValue] = useState(0)
const forceUpdate = () => setForceUpdateValue((value) => value + 1)

useEffect(() => {
toolbar.current = new Toolbar(toolbarRef.current!)
forceUpdate()

return () => toolbar.current?.destroy()
}, [])

return (
<div ref={toolbarRef}>
{Children.map(props.children, (child) =>
cloneElement(child as ReactElement, {
toolbar: toolbar.current,
})
)}
</div>
)
}

interface IToolbarItemProps {
toolbar?: Toolbar
}

interface IToolbarTextProps extends IToolbarItemProps {
text: string
}

export const LunaToolbarText: FC<IToolbarTextProps> = (props) => {
useEffect(() => {
if (props.toolbar) {
props.toolbar.appendText(props.text)
}
}, [props.toolbar])

return null
}

interface IToolbarSelectProps extends IToolbarItemProps {
key: string
value: string
title?: string
options: types.PlainObj<string>
}

export const LunaToolbarSelect: FC<IToolbarSelectProps> = (props) => {
useEffect(() => {
const { toolbar, title, key, value, options } = props
if (toolbar) {
if (title) {
toolbar.appendSelect(key, value, title, options)
} else {
toolbar.appendSelect(key, value, options)
}
}
}, [props.toolbar])

return null
}

export const LunaToolbarSeparator: FC<IToolbarItemProps> = (props) => {
useEffect(() => {
if (props.toolbar) {
props.toolbar.appendSeparator()
}
}, [props.toolbar])

return null
}

export const LunaToolbarSpace: FC<IToolbarItemProps> = (props) => {
useEffect(() => {
if (props.toolbar) {
props.toolbar.appendSpace()
}
}, [props.toolbar])

return null
}

export default LunaToolbar
27 changes: 26 additions & 1 deletion src/toolbar/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import 'luna-toolbar.css'
import Toolbar from 'luna-toolbar.js'
import readme from './README.md'
import story from '../share/story'
import LunaToolbar, {
LunaToolbarText,
LunaToolbarSelect,
LunaToolbarSeparator,
LunaToolbarSpace,
} from './react'

const def = story(
'toolbar',
Expand Down Expand Up @@ -37,9 +43,28 @@ const def = story(
{
readme,
source: __STORY__,
ReactComponent() {
return (
<LunaToolbar>
<LunaToolbarSelect
key="throttling"
value="online"
title="Throttling"
options={{
Online: 'online',
'3G': '3g',
Offline: 'offline',
}}
/>
<LunaToolbarSeparator />
<LunaToolbarSpace />
<LunaToolbarText text="Status: OK" />
</LunaToolbar>
)
},
}
)

export default def

export const { toolbar } = def
export const { toolbar: html, react } = def

0 comments on commit 395b558

Please sign in to comment.