@primer/components 0.0.0-2021926104410 → 0.0.0-2021926114132
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/CHANGELOG.md +4 -14
- package/dist/browser.esm.js +610 -610
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +176 -176
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/Item.d.ts +0 -6
- package/lib/ActionList/Item.js +1 -5
- package/lib/ActionList2/Description.d.ts +6 -0
- package/lib/ActionList2/Description.js +53 -0
- package/lib/ActionList2/Divider.d.ts +8 -0
- package/lib/ActionList2/Divider.js +42 -0
- package/lib/ActionList2/Group.d.ts +8 -0
- package/lib/ActionList2/Group.js +39 -0
- package/lib/ActionList2/Header.d.ts +26 -0
- package/lib/ActionList2/Header.js +55 -0
- package/lib/ActionList2/Item.d.ts +49 -0
- package/lib/ActionList2/Item.js +205 -0
- package/lib/ActionList2/List.d.ts +11 -0
- package/lib/ActionList2/List.js +53 -0
- package/lib/ActionList2/Selection.d.ts +5 -0
- package/lib/ActionList2/Selection.js +67 -0
- package/lib/ActionList2/Visuals.d.ts +11 -0
- package/lib/ActionList2/Visuals.js +90 -0
- package/lib/ActionList2/hacks.d.ts +30 -0
- package/lib/ActionList2/hacks.js +38 -0
- package/lib/ActionList2/index.d.ts +26 -0
- package/lib/ActionList2/index.js +36 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +8 -0
- package/lib/sx.d.ts +2 -0
- package/lib/sx.js +8 -0
- package/lib/utils/create-slots.d.ts +17 -0
- package/lib/utils/create-slots.js +105 -0
- package/lib/utils/use-force-update.d.ts +1 -0
- package/lib/utils/use-force-update.js +19 -0
- package/lib-esm/ActionList/Item.d.ts +0 -6
- package/lib-esm/ActionList/Item.js +1 -5
- package/lib-esm/ActionList2/Description.d.ts +6 -0
- package/lib-esm/ActionList2/Description.js +37 -0
- package/lib-esm/ActionList2/Divider.d.ts +8 -0
- package/lib-esm/ActionList2/Divider.js +30 -0
- package/lib-esm/ActionList2/Group.d.ts +8 -0
- package/lib-esm/ActionList2/Group.js +29 -0
- package/lib-esm/ActionList2/Header.d.ts +26 -0
- package/lib-esm/ActionList2/Header.js +45 -0
- package/lib-esm/ActionList2/Item.d.ts +49 -0
- package/lib-esm/ActionList2/Item.js +198 -0
- package/lib-esm/ActionList2/List.d.ts +11 -0
- package/lib-esm/ActionList2/List.js +35 -0
- package/lib-esm/ActionList2/Selection.d.ts +5 -0
- package/lib-esm/ActionList2/Selection.js +50 -0
- package/lib-esm/ActionList2/Visuals.d.ts +11 -0
- package/lib-esm/ActionList2/Visuals.js +68 -0
- package/lib-esm/ActionList2/hacks.d.ts +30 -0
- package/lib-esm/ActionList2/hacks.js +30 -0
- package/lib-esm/ActionList2/index.d.ts +26 -0
- package/lib-esm/ActionList2/index.js +23 -0
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/sx.d.ts +2 -0
- package/lib-esm/sx.js +3 -1
- package/lib-esm/utils/create-slots.d.ts +17 -0
- package/lib-esm/utils/create-slots.js +84 -0
- package/lib-esm/utils/use-force-update.d.ts +1 -0
- package/lib-esm/utils/use-force-update.js +6 -0
- package/package.json +1 -1
@@ -0,0 +1,84 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { useForceUpdate } from './use-force-update';
|
3
|
+
/** createSlots is a factory that can create a
|
4
|
+
* typesafe Slots + Slot pair to use in a component definition
|
5
|
+
* For example: ActionList.Item uses createSlots to get a Slots wrapper
|
6
|
+
* + Slot component that is used by LeadingVisual, Description
|
7
|
+
*/
|
8
|
+
|
9
|
+
const createSlots = slotNames => {
|
10
|
+
const SlotsContext = /*#__PURE__*/React.createContext({
|
11
|
+
registerSlot: () => null,
|
12
|
+
unregisterSlot: () => null,
|
13
|
+
context: {}
|
14
|
+
});
|
15
|
+
/** Slots uses a Double render strategy inspired by [reach-ui/descendants](https://github.com/reach/reach-ui/tree/develop/packages/descendants)
|
16
|
+
* Slot registers themself with the Slots parent.
|
17
|
+
* When all the children have mounted = registered themselves in slot,
|
18
|
+
* we re-render the parent component to render with slots
|
19
|
+
*/
|
20
|
+
|
21
|
+
const Slots = ({
|
22
|
+
context,
|
23
|
+
children
|
24
|
+
}) => {
|
25
|
+
// initialise slots
|
26
|
+
const slotsDefinition = {};
|
27
|
+
slotNames.map(name => slotsDefinition[name] = null);
|
28
|
+
const slotsRef = React.useRef(slotsDefinition);
|
29
|
+
const rerenderWithSlots = useForceUpdate();
|
30
|
+
const [isMounted, setIsMounted] = React.useState(false); // fires after all the effects in children
|
31
|
+
|
32
|
+
React.useEffect(() => {
|
33
|
+
rerenderWithSlots();
|
34
|
+
setIsMounted(true);
|
35
|
+
}, [rerenderWithSlots]);
|
36
|
+
const registerSlot = React.useCallback((name, contents) => {
|
37
|
+
slotsRef.current[name] = contents; // don't render until the component mounts = all slots are registered
|
38
|
+
|
39
|
+
if (isMounted) rerenderWithSlots();
|
40
|
+
}, [isMounted, rerenderWithSlots]); // Slot can be removed from the tree as well,
|
41
|
+
// we need to unregister them from the slot
|
42
|
+
|
43
|
+
const unregisterSlot = React.useCallback(name => {
|
44
|
+
slotsRef.current[name] = null;
|
45
|
+
rerenderWithSlots();
|
46
|
+
}, [rerenderWithSlots]);
|
47
|
+
/**
|
48
|
+
* Slots uses a render prop API so abstract the
|
49
|
+
* implementation detail of using a context provider.
|
50
|
+
*/
|
51
|
+
|
52
|
+
const slots = slotsRef.current;
|
53
|
+
return /*#__PURE__*/React.createElement(SlotsContext.Provider, {
|
54
|
+
value: {
|
55
|
+
registerSlot,
|
56
|
+
unregisterSlot,
|
57
|
+
context
|
58
|
+
}
|
59
|
+
}, children(slots));
|
60
|
+
};
|
61
|
+
|
62
|
+
const Slot = ({
|
63
|
+
name,
|
64
|
+
children
|
65
|
+
}) => {
|
66
|
+
const {
|
67
|
+
registerSlot,
|
68
|
+
unregisterSlot,
|
69
|
+
context
|
70
|
+
} = React.useContext(SlotsContext);
|
71
|
+
React.useEffect(() => {
|
72
|
+
registerSlot(name, typeof children === 'function' ? children(context) : children);
|
73
|
+
return () => unregisterSlot(name);
|
74
|
+
}, [name, children, registerSlot, unregisterSlot, context]);
|
75
|
+
return null;
|
76
|
+
};
|
77
|
+
|
78
|
+
return {
|
79
|
+
Slots,
|
80
|
+
Slot
|
81
|
+
};
|
82
|
+
};
|
83
|
+
|
84
|
+
export default createSlots;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const useForceUpdate: () => () => void;
|
@@ -0,0 +1,6 @@
|
|
1
|
+
// Inspired from reach-ui: https://github.com/reach/reach-ui/blob/develop/packages/utils/src/use-force-update.ts
|
2
|
+
import React from 'react';
|
3
|
+
export const useForceUpdate = () => {
|
4
|
+
const [, rerender] = React.useState({});
|
5
|
+
return React.useCallback(() => rerender({}), []);
|
6
|
+
};
|