@startlibs/core 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +476 -478
- package/package.json +5 -2
- package/rollup.config.js +11 -1
- package/src/SlotFill.jsx +165 -0
- package/src/StartlibsProvider.jsx +61 -0
- package/src/hocs/WithLazyResource.jsx +32 -0
- package/src/hocs/WithRef.js +20 -0
- package/src/hocs/Wrapper.jsx +40 -0
- package/src/hocs/consumes.jsx +17 -0
- package/src/hocs/withDialogError.jsx +16 -0
- package/src/hocs/withGroupEvent.jsx +43 -0
- package/src/hooks/WithConstant.jsx +7 -0
- package/src/hooks/WithEffect.jsx +6 -0
- package/src/hooks/useAsyncEffect.jsx +11 -0
- package/src/hooks/useCallState.js +15 -0
- package/src/hooks/useCallbackStable.jsx +12 -0
- package/src/hooks/useConstant.js +20 -0
- package/src/hooks/useConsumerCatalog.jsx +21 -0
- package/src/hooks/useEnsureRef.js +7 -0
- package/src/hooks/useForceUpdate.jsx +16 -0
- package/src/hooks/useIsUnmounted.jsx +12 -0
- package/src/hooks/usePopupToggle.js +30 -0
- package/src/hooks/usePrevious.jsx +27 -0
- package/src/hooks/useRecycleFocus.jsx +28 -0
- package/src/hooks/useRefMemo.jsx +10 -0
- package/src/hooks/useRefState.js +26 -0
- package/src/hooks/useSignal.jsx +193 -0
- package/src/hooks/useTitle.jsx +12 -0
- package/src/hooks/useToggle.js +124 -0
- package/src/hooks/useUpdateEffect.js +14 -0
- package/src/hooks/useUpdateOnResize.jsx +14 -0
- package/src/hooks/willUseLayers.jsx +42 -0
- package/src/hooks/willUseSharedCallback.jsx +61 -0
- package/src/hooks/willUseSharedState.jsx +34 -0
- package/src/hooks/willUseSpreadState.jsx +101 -0
- package/src/index.js +31 -0
- package/yarn-error.log +3374 -0
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@startlibs/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Startlibs Core",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"source": true,
|
|
8
8
|
"peerDependencies": {
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
"@babel/plugin-proposal-class-properties": "^7.2.1",
|
|
19
19
|
"@babel/plugin-proposal-export-namespace-from": "^7.2.0",
|
|
20
20
|
"@babel/plugin-proposal-json-strings": "^7.2.0",
|
|
21
|
+
"@babel/plugin-proposal-logical-assignment-operators": "^7.20.7",
|
|
22
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
21
23
|
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
|
|
22
24
|
"@babel/plugin-proposal-optional-chaining": "^7.12.0",
|
|
23
25
|
"@babel/plugin-proposal-throw-expressions": "^7.2.0",
|
|
@@ -28,6 +30,7 @@
|
|
|
28
30
|
"@babel/preset-env": "^7.2.0",
|
|
29
31
|
"@babel/preset-flow": "^7.0.0",
|
|
30
32
|
"@babel/preset-react": "^7.0.0",
|
|
33
|
+
"@rollup/plugin-alias": "^5.1.1",
|
|
31
34
|
"babel-plugin-lodash": "^3.3.4",
|
|
32
35
|
"babel-plugin-macros": "^2.4.2",
|
|
33
36
|
"babel-plugin-styled-components": "^1.10.0",
|
package/rollup.config.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import resolve from 'rollup-plugin-node-resolve';
|
|
2
2
|
import commonjs from 'rollup-plugin-commonjs';
|
|
3
3
|
import babel from 'rollup-plugin-babel';
|
|
4
|
+
import alias from '@rollup/plugin-alias';
|
|
5
|
+
import path from 'path'
|
|
4
6
|
|
|
5
7
|
const external = id => !id.startsWith('.') && !id.startsWith('/') && !id.startsWith('\0');
|
|
8
|
+
const projectRootDir = path.resolve(__dirname);
|
|
6
9
|
|
|
7
10
|
// rollup.config.js
|
|
8
11
|
export default {
|
|
9
12
|
input: 'src/index.js',
|
|
10
13
|
output: {
|
|
11
14
|
file: './lib/index.js',
|
|
12
|
-
format: '
|
|
15
|
+
format: 'es'
|
|
13
16
|
},
|
|
14
17
|
plugins: [
|
|
15
18
|
resolve({ extensions: ['.jsx', '.js'] }),
|
|
@@ -35,8 +38,15 @@ export default {
|
|
|
35
38
|
'@babel/plugin-transform-arrow-functions',
|
|
36
39
|
'@babel/plugin-transform-spread',
|
|
37
40
|
'@babel/plugin-proposal-object-rest-spread',
|
|
41
|
+
'@babel/plugin-proposal-nullish-coalescing-operator',
|
|
42
|
+
'@babel/plugin-proposal-logical-assignment-operators',
|
|
38
43
|
['@babel/plugin-proposal-class-properties', {'loose': true}]
|
|
39
44
|
]
|
|
45
|
+
}),
|
|
46
|
+
alias({
|
|
47
|
+
entries:{
|
|
48
|
+
'@startlibs/core':'test'
|
|
49
|
+
}
|
|
40
50
|
})
|
|
41
51
|
],
|
|
42
52
|
external//: [ "react", "react-dom", "styled-components", "recompose"]
|
package/src/SlotFill.jsx
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import React, {useContext, useEffect, useLayoutEffect, useMemo, useRef} from 'react'
|
|
2
|
+
import ReactDOM from 'react-dom'
|
|
3
|
+
import styled from 'styled-components'
|
|
4
|
+
import _ from 'lodash/fp'
|
|
5
|
+
import {_s, callIfFunction} from '@startlibs/utils'
|
|
6
|
+
import {willUseSharedState, useConstant} from '@startlibs/core'
|
|
7
|
+
|
|
8
|
+
export const SlotContext = React.createContext(new Map())
|
|
9
|
+
const [useFillHookState, useFillHookStateRef] = willUseSharedState()
|
|
10
|
+
const [useHiddenFillHookState, useHiddenFillHookStateRef] = willUseSharedState()
|
|
11
|
+
|
|
12
|
+
const DEFAULT_CONTEXT = new Map()
|
|
13
|
+
|
|
14
|
+
export const SlotProvider = ({children}) => {
|
|
15
|
+
|
|
16
|
+
return <SlotContext.Provider value={DEFAULT_CONTEXT}>
|
|
17
|
+
{children}
|
|
18
|
+
<React.Suspense fallback={null}><FillHooks/></React.Suspense>
|
|
19
|
+
<HiddenFillHooks/>
|
|
20
|
+
</SlotContext.Provider>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const Slot = ({name,children,...rest}) => {
|
|
24
|
+
|
|
25
|
+
const ref = useRef()
|
|
26
|
+
const slotInfo = useConstant({ref})
|
|
27
|
+
|
|
28
|
+
const slots = useContext(SlotContext)
|
|
29
|
+
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
const previous = slots.get(name)
|
|
32
|
+
const updates = previous?.updates || []
|
|
33
|
+
if (previous?.children) {
|
|
34
|
+
// reparent
|
|
35
|
+
}
|
|
36
|
+
slots.set(name,_.flow(
|
|
37
|
+
_.update('infos',_.union([slotInfo]))
|
|
38
|
+
)(previous))
|
|
39
|
+
updates.forEach(v => v({}))
|
|
40
|
+
return () => {
|
|
41
|
+
const previous = slots.get(name)
|
|
42
|
+
previous.infos = _.without([slotInfo],previous.infos)
|
|
43
|
+
if (!previous.infos?.length && !previous.updates?.length) {
|
|
44
|
+
slots.delete(name)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}, [name])
|
|
48
|
+
|
|
49
|
+
return children
|
|
50
|
+
? <>
|
|
51
|
+
<div data-id={name} {...rest} ref={ref}/>
|
|
52
|
+
{React.Children.map(children,c => React.cloneElement(c,{...c.props,name,stack:Number.MIN_SAFE_INTEGER}))}
|
|
53
|
+
</>
|
|
54
|
+
: <div data-id={name} {...rest} ref={ref}/>
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const Invisible = styled.div`display:none`
|
|
59
|
+
|
|
60
|
+
const FillBeforeFlip = ({name, children, slot}) => {
|
|
61
|
+
const slots = useContext(SlotContext)
|
|
62
|
+
const ref = useRef()
|
|
63
|
+
useLayoutEffect(() => {
|
|
64
|
+
const infos = slots.get(name)?.infos || []
|
|
65
|
+
const mounted = _.flatMap(({ref}) => ref?.current,infos)
|
|
66
|
+
const clones = []
|
|
67
|
+
const getClone = () => {
|
|
68
|
+
const cloned = ref.current.cloneNode(true)
|
|
69
|
+
cloned.className = 'JS-fill-flip'
|
|
70
|
+
clones.push(cloned)
|
|
71
|
+
return cloned
|
|
72
|
+
}
|
|
73
|
+
mounted.map(_ => _.appendChild(getClone()))
|
|
74
|
+
return () => {
|
|
75
|
+
clones.forEach(el => el.remove())
|
|
76
|
+
}
|
|
77
|
+
},[])
|
|
78
|
+
return <Invisible ref={ref}>{children}</Invisible>
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const useFillState = (name,stack) => {
|
|
82
|
+
const forceUpdate = React.useState(false)[1];
|
|
83
|
+
const slots = useContext(SlotContext)
|
|
84
|
+
const renderSlot = slots.get(name)
|
|
85
|
+
const maxStack = _.max(renderSlot?.stacks)
|
|
86
|
+
const onStack = !_.isFinite(maxStack) || maxStack === stack
|
|
87
|
+
useLayoutEffect(() => {
|
|
88
|
+
|
|
89
|
+
const slot = slots.get(name)
|
|
90
|
+
const updatedSlot = _.flow(
|
|
91
|
+
_.update('updates',_.union([forceUpdate])),
|
|
92
|
+
_.update('stacks',_.union([stack])),
|
|
93
|
+
)(slot)
|
|
94
|
+
slots.set(name, updatedSlot)
|
|
95
|
+
updatedSlot.updates.forEach(v => v({}))
|
|
96
|
+
return () => {
|
|
97
|
+
const previous = slots.get(name)
|
|
98
|
+
const index = previous.updates.indexOf(forceUpdate)
|
|
99
|
+
const next = _.flow(
|
|
100
|
+
_.update('updates',_s.removeAt(index)),
|
|
101
|
+
_.update('stacks',_s.removeAt(index))
|
|
102
|
+
)(previous)
|
|
103
|
+
slots.set(name,next)
|
|
104
|
+
next.updates.forEach(v => v({}))
|
|
105
|
+
}
|
|
106
|
+
},[])
|
|
107
|
+
|
|
108
|
+
return [renderSlot,onStack]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
export const Fill = ({name, children, stack = 0, mountBeforeFlip}) => {
|
|
113
|
+
const [slot,onStack] = useFillState(name,stack)
|
|
114
|
+
if (slot?.infos && onStack) {
|
|
115
|
+
return slot?.infos.map((info) => info.ref?.current && ReactDOM.createPortal(callIfFunction(children,info),info.ref.current))
|
|
116
|
+
} else if (mountBeforeFlip) {
|
|
117
|
+
return <FillBeforeFlip name={name} slot={slot}>{children}</FillBeforeFlip>
|
|
118
|
+
} else {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
const FillHooks = ({}) => {
|
|
125
|
+
const [hooked] = useFillHookState({})
|
|
126
|
+
return Object.entries(hooked)
|
|
127
|
+
.map(([name, childs]) => childs.map(({children}, i) => <Fill key={name + i} name={name}>{children}</Fill>))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let uid = 0
|
|
131
|
+
export const useFill = (name, children, inputs) => {
|
|
132
|
+
const [, set] = useFillHookStateRef()
|
|
133
|
+
const key = useRef(uid++)
|
|
134
|
+
useLayoutEffect(() => {
|
|
135
|
+
if (children) {
|
|
136
|
+
set(_.update([name], (list = []) => {
|
|
137
|
+
const index = _.findIndex(['key',key.current],list)
|
|
138
|
+
const value = {children,key:key.current}
|
|
139
|
+
if (index >= 0 ) {
|
|
140
|
+
return _.set([index],value,list)
|
|
141
|
+
} else {
|
|
142
|
+
return list.concat(value)
|
|
143
|
+
}
|
|
144
|
+
} ))
|
|
145
|
+
} else {
|
|
146
|
+
set(_.update([name], _.differenceBy('key',_, [{children,key:key.current}])))
|
|
147
|
+
}
|
|
148
|
+
}, inputs || children)
|
|
149
|
+
|
|
150
|
+
useLayoutEffect(() =>
|
|
151
|
+
() => set(_.update([name], _.differenceBy('key',_, [{key:key.current}])))
|
|
152
|
+
,[])
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const HiddenFillHooks = ({}) => {
|
|
156
|
+
const [hookedChildren] = useHiddenFillHookState({})
|
|
157
|
+
return <div>{Object.values(hookedChildren)}</div>
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export const useHiddenFill = (children, inputs) => {
|
|
161
|
+
const [, set] = useHiddenFillHookStateRef()
|
|
162
|
+
const [uid] = useMemo(()=>_.uniqueId(""),[])
|
|
163
|
+
set(_.set([uid], children ? React.cloneElement(children,{key:uid}) : null))
|
|
164
|
+
useEffect(()=> () => set(_.unset([uid])),[])
|
|
165
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import _ from 'lodash/fp'
|
|
3
|
+
import {fixMediaQuery,callIfFunction} from '@startlibs/utils'
|
|
4
|
+
import {SlotProvider, clearPopups} from '@startlibs/core'
|
|
5
|
+
|
|
6
|
+
export const StartlibsContext = React.createContext({})
|
|
7
|
+
export const PopupContext = React.createContext({})
|
|
8
|
+
export const StartlibsMetacontext = React.createContext(()=>{})
|
|
9
|
+
|
|
10
|
+
export const StartlibsConsumer = StartlibsContext.Consumer
|
|
11
|
+
|
|
12
|
+
const wrappeStyle = {height: '100%'}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export class StartlibsProvider extends React.Component {
|
|
16
|
+
|
|
17
|
+
componentDidMount() {
|
|
18
|
+
fixMediaQuery()
|
|
19
|
+
const addVHFix = () => {
|
|
20
|
+
let vh = window.innerHeight * 0.01;
|
|
21
|
+
document.documentElement?.style?.setProperty('--vh', `${vh}px`);
|
|
22
|
+
}
|
|
23
|
+
window.addEventListener('resize', _.debounce(200, addVHFix))
|
|
24
|
+
addVHFix()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
openToggles = new Map()
|
|
28
|
+
|
|
29
|
+
popupToggleRender = ({isOpen,close},ref) => {
|
|
30
|
+
if (isOpen && !this.openToggles.has(ref)) {
|
|
31
|
+
this.closePopup()
|
|
32
|
+
this.openToggles.set(ref,close)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
closePopup = () => {
|
|
36
|
+
clearPopups()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
startlibsMeta = new Map()
|
|
40
|
+
|
|
41
|
+
getMeta = (key,initValue) => {
|
|
42
|
+
const exists = this.startlibsMeta.get(key)
|
|
43
|
+
if (exists) return exists
|
|
44
|
+
this.startlibsMeta.set(key,callIfFunction(initValue))
|
|
45
|
+
return initValue
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
render() {
|
|
49
|
+
return <StartlibsContext.Provider value={this.props.value}>
|
|
50
|
+
<StartlibsMetacontext.Provider value={this.getMeta}>
|
|
51
|
+
<PopupContext.Provider value={this.popupToggleRender}>
|
|
52
|
+
<div style={wrappeStyle} onClick={this.closePopup}>
|
|
53
|
+
<SlotProvider>
|
|
54
|
+
{this.props.children}
|
|
55
|
+
</SlotProvider>
|
|
56
|
+
</div>
|
|
57
|
+
</PopupContext.Provider>
|
|
58
|
+
</StartlibsMetacontext.Provider>
|
|
59
|
+
</StartlibsContext.Provider>
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, {useReducer} from 'react'
|
|
2
|
+
import _ from 'lodash/fp'
|
|
3
|
+
import {ifUndefined} from '@startlibs/utils'
|
|
4
|
+
|
|
5
|
+
const addExtraProp = (element, extra, override = true) => React.cloneElement(
|
|
6
|
+
element,
|
|
7
|
+
override ? {...element.props,...extra} : {...extra,...element.props},
|
|
8
|
+
element.props.children
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
export const WithLazyPlaceholder = ({value, fallback, children, forwardProps = false,...props}) => {
|
|
12
|
+
return <React.Suspense
|
|
13
|
+
fallback={React.Children.map(children(fallback,{isLoading:true}), (child) => React.isValidElement(child) ? addExtraProp(child, {isLoading: true}, false) : child)}
|
|
14
|
+
>
|
|
15
|
+
<Load value={value} children={children} forwardProps={forwardProps && props}/>
|
|
16
|
+
</React.Suspense>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export const WithLazyResource = ({value, wrap,fallback,children,forwardProps = false,...props}) => {
|
|
21
|
+
|
|
22
|
+
return (wrap || fallback !== undefined)
|
|
23
|
+
? <React.Suspense fallback={ifUndefined(fallback,null)}><Load value={value} forwardProps={forwardProps && props} children={children}/></React.Suspense>
|
|
24
|
+
: <Load value={value} children={children} forwardProps={forwardProps && props}/>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const Load = ({value,children,forwardProps}) => {
|
|
28
|
+
const loaded = _.isArray(value) ? (value.forEach(v => v.promise()) || value.map(v => v.read())) : value.read()
|
|
29
|
+
const [,update] = useReducer((s) => s+1, 0)
|
|
30
|
+
const refresh = () => Promise.all(_.isArray(value) ? value.map(v => v.refresh().promise()) : [value.refresh().promise()]).then(update)
|
|
31
|
+
return forwardProps ? addExtraProp(children(loaded,{refresh,update}),forwardProps) : children(loaded, {refresh,update})
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
4
|
+
export class WithRef extends React.Component {
|
|
5
|
+
|
|
6
|
+
constructor(props) {
|
|
7
|
+
super(props)
|
|
8
|
+
const total = props.length || props.children.length
|
|
9
|
+
const refs = new Array(total)
|
|
10
|
+
for (var i = 0; i < total; i++) {
|
|
11
|
+
refs[i] = React.createRef()
|
|
12
|
+
}
|
|
13
|
+
this.createdRefs = refs
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
render() {
|
|
17
|
+
const {children} = this.props
|
|
18
|
+
return children(...this.createdRefs)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
const valueOrDefault = (v, d) => v === undefined ? d : v
|
|
4
|
+
const mergePropsAndChildren = (a, b) => ({
|
|
5
|
+
...a, ...b,
|
|
6
|
+
children: (a.children && b.children) ?
|
|
7
|
+
<React.Fragment>{a.children}{b.children}</React.Fragment> : (a.children || b.children)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
const wrapIf = (WrapperWithRenderProps, testKeyOrFn, defaultWrapPropsOrFn) => (functionAsChild) => (props) => {
|
|
11
|
+
const test = typeof testKeyOrFn === 'string' ? props[testKeyOrFn] : testKeyOrFn(props)
|
|
12
|
+
if (test) {
|
|
13
|
+
const defaultWrapProps = typeof defaultWrapPropsOrFn === "function" ? defaultWrapPropsOrFn(props) : defaultWrapPropsOrFn
|
|
14
|
+
if (WrapperWithRenderProps.hasRenderProps) {
|
|
15
|
+
const Wrapper = WrapperWithRenderProps.Wrapper
|
|
16
|
+
return <Wrapper {...defaultWrapProps} {...props} ref={undefined}>{(extra) => functionAsChild(mergePropsAndChildren(props, extra))}</Wrapper>
|
|
17
|
+
} else {
|
|
18
|
+
return <WrapperWithRenderProps {...defaultWrapProps} {...props} ref={undefined}>{functionAsChild(props)}</WrapperWithRenderProps>
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
return functionAsChild(props)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const wrapAsDefault = (Wrapper, key, defaultWrapProps) => wrapIf(Wrapper, (props) => valueOrDefault(props[key], true), defaultWrapProps)
|
|
27
|
+
|
|
28
|
+
const singleWrap = (Wrapper, defaultWrapProps) => wrapIf(Wrapper, () => true, defaultWrapProps)
|
|
29
|
+
|
|
30
|
+
const wraps = (...autoWrappers) => (Component) => autoWrappers.reduceRight(
|
|
31
|
+
(acc, autoWrapper) => autoWrapper(acc),
|
|
32
|
+
(props) => <Component {...props}/>
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
export const wrap = wraps
|
|
36
|
+
|
|
37
|
+
wrap.renderProps = (Wrapper) => ({hasRenderProps:true,Wrapper})
|
|
38
|
+
wrap.if = wrapIf
|
|
39
|
+
wrap.always = singleWrap
|
|
40
|
+
wrap.asDefault = wrapAsDefault
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import styled from 'styled-components'
|
|
3
|
+
|
|
4
|
+
const propsOrConsume = (prop, Consumer,childrenAsFunction) => prop ? childrenAsFunction(prop) : <Consumer>{childrenAsFunction}</Consumer>
|
|
5
|
+
|
|
6
|
+
const addConsumers = (Component,obj) =>
|
|
7
|
+
Object.entries(obj)
|
|
8
|
+
.reduce(
|
|
9
|
+
(acc, [propKey, Consumer]) => (props) =>
|
|
10
|
+
propsOrConsume(props[propKey],Consumer,v => acc({[propKey]: v, ...props})),
|
|
11
|
+
(props) => <Component {...props} />
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
export const consumes = (obj) => (Component) => {
|
|
15
|
+
const withConsumers = addConsumers(Component,obj)
|
|
16
|
+
return (props) => withConsumers(props)
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
export const withDialogError = (Dialog) => {
|
|
4
|
+
const ErrorBoundedComponent = React.forwardRef((props,ref) => {
|
|
5
|
+
return <Dialog ref={ref} {...props}/>
|
|
6
|
+
|
|
7
|
+
// const ErrorBoundary = __internals('ErrorBoundary')
|
|
8
|
+
/*return (
|
|
9
|
+
<ErrorBoundary Error={props.Error || __internals('DialogError')} closeDialog={props.closeDialog}>
|
|
10
|
+
<Dialog {...props}/>
|
|
11
|
+
</ErrorBoundary>
|
|
12
|
+
)*/
|
|
13
|
+
})
|
|
14
|
+
// ErrorBoundedComponent.contextTypes = {startlibs:()=>null}
|
|
15
|
+
return ErrorBoundedComponent
|
|
16
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import mitt from 'mitt'
|
|
3
|
+
|
|
4
|
+
export const GroupEventFactory = () => {
|
|
5
|
+
let emitter = mitt()
|
|
6
|
+
|
|
7
|
+
class WithGroupEvent extends React.Component {
|
|
8
|
+
componentDidMount() {
|
|
9
|
+
emitter.on('*', this.processEvent)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
componentWillUnmount() {
|
|
13
|
+
emitter.off('*',this.processEvent)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
events = {}
|
|
17
|
+
|
|
18
|
+
processEvent = (type, event) => {
|
|
19
|
+
(this.events[type] || []).forEach(c => c(event))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
groupEventProps = {
|
|
23
|
+
on: (type,callback) => {
|
|
24
|
+
this.events[type] = (this.events[type] || []).concat(callback)
|
|
25
|
+
},
|
|
26
|
+
off: (type,callback) => {
|
|
27
|
+
this.events[type] = (this.events[type] || []).filter(e=>e!==callback)
|
|
28
|
+
},
|
|
29
|
+
emit: emitter.emit
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
render() {
|
|
33
|
+
return this.props.children(this.groupEventProps)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return WithGroupEvent
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const withGroupEvent = (Component) => {
|
|
40
|
+
const WithGroupEvent = GroupEventFactory()
|
|
41
|
+
return React.forwardRef((props,ref) => <WithGroupEvent>{(groupEvent) => <Component {...props} ref={ref} groupEvent={groupEvent}/>}</WithGroupEvent>)
|
|
42
|
+
}
|
|
43
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, {useMemo, useState} from 'react'
|
|
2
|
+
import {_s, callIfFunction, callIfFunctions} from '@startlibs/utils'
|
|
3
|
+
|
|
4
|
+
const isPath = (path) => typeof path === 'string' || typeof path === 'number' || Array.isArray(path)
|
|
5
|
+
|
|
6
|
+
const getter = (value) => path => isPath(path) ? _s.get(path, value) : value
|
|
7
|
+
|
|
8
|
+
export const useCallState = (defaultValue, setCallback) => {
|
|
9
|
+
const [val, setValue] = useState(defaultValue)
|
|
10
|
+
|
|
11
|
+
return useMemo(() => {
|
|
12
|
+
const get = () => val
|
|
13
|
+
return {get, val, set: setValue}
|
|
14
|
+
}, [val])
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React, {useInsertionEffect} from 'react'
|
|
2
|
+
|
|
3
|
+
export const useCallbackStable = (fn) => {
|
|
4
|
+
const ref = React.useRef([fn, (...args) => ref[0](...args)]).current;
|
|
5
|
+
// Per Dan Abramov: useInsertionEffect executes marginally closer to the
|
|
6
|
+
// correct timing for ref synchronization than useLayoutEffect on React 18.
|
|
7
|
+
// See: https://github.com/facebook/react/pull/25881#issuecomment-1356244360
|
|
8
|
+
useInsertionEffect(() => {
|
|
9
|
+
ref[0] = fn;
|
|
10
|
+
});
|
|
11
|
+
return ref[1];
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React, {useMemo, useRef} from 'react'
|
|
2
|
+
import {_s, callIfFunction, callIfFunctions} from '@startlibs/utils'
|
|
3
|
+
|
|
4
|
+
const not_defined = {}
|
|
5
|
+
|
|
6
|
+
export const useConstant = (value) => {
|
|
7
|
+
const ref = useRef(not_defined)
|
|
8
|
+
if (ref.current === not_defined) {
|
|
9
|
+
ref.current = value
|
|
10
|
+
}
|
|
11
|
+
return ref.current
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const useLazyConstant= (value) => {
|
|
15
|
+
const ref = useRef(not_defined)
|
|
16
|
+
if (ref.current === not_defined) {
|
|
17
|
+
ref.current = callIfFunction(value)
|
|
18
|
+
}
|
|
19
|
+
return ref.current
|
|
20
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React, {useEffect, useMemo} from 'react'
|
|
2
|
+
import {callMany,mapGetOrSet} from '@startlibs/utils'
|
|
3
|
+
import {useRefState} from '@startlibs/core'
|
|
4
|
+
|
|
5
|
+
export const useConsumerCatalog = (mountCallback,unmountCallback) => {
|
|
6
|
+
const elements = useRefState([])
|
|
7
|
+
|
|
8
|
+
const register = callMany((element) => elements.set((elements) => elements.concat([element])), mountCallback)
|
|
9
|
+
const unregister = callMany((element) => elements.set((elements) => elements.filter(_ => _ !== element)), unmountCallback)
|
|
10
|
+
|
|
11
|
+
const manager = useMemo(()=> ({register,unregister}), [])
|
|
12
|
+
|
|
13
|
+
return [elements.get,manager]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const useCatalogManager = ({register,unregister},ref) => {
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
register(ref)
|
|
19
|
+
return () => unregister(ref)
|
|
20
|
+
},[ref])
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {useState} from 'react'
|
|
2
|
+
import {willUseSharedState} from '@startlibs/core'
|
|
3
|
+
|
|
4
|
+
export const useForceUpdate = () => {
|
|
5
|
+
const [value, setValue] = useState(0); // integer state
|
|
6
|
+
return () => setValue(value => value + 1); // update the state to force render
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const willUseForceUpdate = () => {
|
|
10
|
+
const [useValue,,set] = willUseSharedState()
|
|
11
|
+
const useForceUpdate = () => {
|
|
12
|
+
const [v,setV] = useValue(0)
|
|
13
|
+
return () => setV(value => value + 1)
|
|
14
|
+
}
|
|
15
|
+
return [useForceUpdate,() => set(value => value + 1)]
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {useEffect, useRef} from 'react'
|
|
2
|
+
|
|
3
|
+
export const useIsUnmounted = () => {
|
|
4
|
+
const unmounted = useRef(false)
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
unmounted.current = false
|
|
8
|
+
return () => { unmounted.current = true }
|
|
9
|
+
}, [])
|
|
10
|
+
|
|
11
|
+
return () => unmounted.current
|
|
12
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React, {useRef, useContext} from 'react'
|
|
2
|
+
import {useToggle} from '@startlibs/core'
|
|
3
|
+
import {callIfFunction} from '@startlibs/utils'
|
|
4
|
+
|
|
5
|
+
let prevOpenToggleClose = () => {}
|
|
6
|
+
let sameTick = false
|
|
7
|
+
|
|
8
|
+
export const clearPopups = () => {
|
|
9
|
+
if (!sameTick) {
|
|
10
|
+
prevOpenToggleClose()
|
|
11
|
+
prevOpenToggleClose = () => {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const usePopupToggle = (startWith,callback) => {
|
|
16
|
+
const toggle = useToggle(startWith,(v,prev) => {
|
|
17
|
+
const result = callIfFunction(callback,v,prev)
|
|
18
|
+
if (result === false) { return false}
|
|
19
|
+
if (v !== false && prevOpenToggleClose !== toggle.close) {
|
|
20
|
+
prevOpenToggleClose()
|
|
21
|
+
prevOpenToggleClose = toggle.close
|
|
22
|
+
sameTick = true
|
|
23
|
+
window.setTimeout(()=>sameTick = false, 1)
|
|
24
|
+
}
|
|
25
|
+
if (v === false) {
|
|
26
|
+
prevOpenToggleClose = () => {}
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
return toggle
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {useEffect, useRef} from 'react'
|
|
2
|
+
|
|
3
|
+
export function usePrevious(value,initialValue) {
|
|
4
|
+
const ref = useRef(initialValue);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
ref.current = value;
|
|
7
|
+
});
|
|
8
|
+
return ref.current;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function usePreviousUnique(value,initialValue) {
|
|
12
|
+
const ref = useRef(initialValue);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (value !== ref.current) {
|
|
15
|
+
ref.current = value;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return ref.current;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function usePreviousForEffect(value,initialValue) {
|
|
22
|
+
const ref = useRef(initialValue);
|
|
23
|
+
useEffect(() => () => {
|
|
24
|
+
ref.current = value;
|
|
25
|
+
},[value]);
|
|
26
|
+
return ref.current;
|
|
27
|
+
}
|