1 | import React, { useLayoutEffect } from 'react'
|
2 | import {
|
3 | observer,
|
4 | useValue,
|
5 | useComponentId,
|
6 | useLocal,
|
7 | useBind
|
8 | } from 'startupjs'
|
9 | import { Dimensions, StyleSheet } from 'react-native'
|
10 | import propTypes from 'prop-types'
|
11 | import Sidebar from '../Sidebar'
|
12 | import DrawerSidebar from '../DrawerSidebar'
|
13 | import config from '../../config/rootConfig'
|
14 |
|
15 | const FIXED_LAYOUT_BREAKPOINT = 1024
|
16 |
|
17 | function SmartSidebar ({
|
18 | style,
|
19 | forceClosed,
|
20 | fixedLayoutBreakpoint,
|
21 | path,
|
22 | $open,
|
23 | position,
|
24 | width,
|
25 | backgroundColor,
|
26 | children,
|
27 | renderContent,
|
28 | ...props
|
29 | }) {
|
30 | if (path) {
|
31 | console.warn('[@startupjs/ui] Sidebar: path is DEPRECATED, use $open instead.')
|
32 | }
|
33 |
|
34 | if (/^#|rgb/.test(backgroundColor)) {
|
35 | console.warn('[@startupjs/ui] Sidebar:: Hex color for backgroundColor property is deprecated. Use style instead')
|
36 | }
|
37 |
|
38 | const componentId = useComponentId()
|
39 | if (!$open) {
|
40 | [, $open] = useLocal(path || `_session.SmartSidebar.${componentId}`)
|
41 | }
|
42 |
|
43 | ;({ backgroundColor = config.colors.white, ...style } = StyleSheet.flatten([
|
44 | { backgroundColor: config.colors[backgroundColor] || backgroundColor },
|
45 | style
|
46 | ]))
|
47 |
|
48 | let open
|
49 | let onChange
|
50 | ;({ open, onChange } = useBind({ $open: $open, open, onChange }))
|
51 |
|
52 | let [fixedLayout, $fixedLayout] = useValue(isFixedLayout(fixedLayoutBreakpoint))
|
53 |
|
54 | useLayoutEffect(() => {
|
55 | Dimensions.addEventListener('change', handleWidthChange)
|
56 | return () => Dimensions.removeEventListener('change', handleWidthChange)
|
57 | }, [])
|
58 |
|
59 | function handleWidthChange () {
|
60 | $fixedLayout.setDiff(isFixedLayout(fixedLayoutBreakpoint))
|
61 | }
|
62 |
|
63 | return pug`
|
64 | if fixedLayout
|
65 | Sidebar(
|
66 | style=style
|
67 | $open=$open
|
68 | position=position
|
69 | width=width
|
70 | forceClosed=forceClosed
|
71 | backgroundColor=backgroundColor
|
72 | renderContent=renderContent
|
73 | )= children
|
74 | else
|
75 | DrawerSidebar(
|
76 | style=style
|
77 | $open=$open
|
78 | position=position
|
79 | width=width
|
80 | forceClosed=forceClosed
|
81 | backgroundColor=backgroundColor
|
82 | renderContent=renderContent
|
83 | ...props
|
84 | )= children
|
85 | `
|
86 | }
|
87 |
|
88 | SmartSidebar.defaultProps = {
|
89 | forceClosed: false,
|
90 | fixedLayoutBreakpoint: FIXED_LAYOUT_BREAKPOINT,
|
91 | position: 'left',
|
92 | width: 264
|
93 | }
|
94 |
|
95 | SmartSidebar.propTypes = {
|
96 | style: propTypes.oneOfType([propTypes.object, propTypes.array]),
|
97 | children: propTypes.node,
|
98 | $open: propTypes.object,
|
99 | forceClosed: propTypes.bool,
|
100 | fixedLayoutBreakpoint: propTypes.number,
|
101 | position: propTypes.oneOf(['left', 'right']),
|
102 | width: propTypes.number,
|
103 | renderContent: propTypes.func
|
104 | }
|
105 |
|
106 | export default observer(SmartSidebar)
|
107 |
|
108 | function isFixedLayout (fixedLayoutBreakpoint) {
|
109 | let dim = Dimensions.get('window')
|
110 | return dim.width > fixedLayoutBreakpoint
|
111 | }
|