@primer/components 31.2.0-rc.5ccefd7d → 31.2.0-rc.6afeaa37

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.
Files changed (143) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/browser.esm.js +622 -620
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +163 -161
  5. package/dist/browser.umd.js.map +1 -1
  6. package/docs/content/ActionList2.mdx +354 -0
  7. package/docs/content/getting-started.md +1 -1
  8. package/docs/src/@primer/gatsby-theme-doctocat/components/hero.js +1 -3
  9. package/docs/src/@primer/gatsby-theme-doctocat/components/live-preview-wrapper.js +1 -1
  10. package/docs/src/@primer/gatsby-theme-doctocat/live-code-scope.js +17 -0
  11. package/lib/ActionList2/Description.d.ts +12 -0
  12. package/lib/ActionList2/Description.js +53 -0
  13. package/lib/ActionList2/Divider.d.ts +5 -0
  14. package/lib/ActionList2/Divider.js +35 -0
  15. package/lib/ActionList2/Group.d.ts +11 -0
  16. package/lib/ActionList2/Group.js +57 -0
  17. package/lib/ActionList2/Header.d.ts +26 -0
  18. package/lib/ActionList2/Header.js +55 -0
  19. package/lib/ActionList2/Item.d.ts +63 -0
  20. package/lib/ActionList2/Item.js +244 -0
  21. package/lib/ActionList2/LinkItem.d.ts +17 -0
  22. package/lib/ActionList2/LinkItem.js +57 -0
  23. package/lib/ActionList2/List.d.ts +26 -0
  24. package/lib/ActionList2/List.js +59 -0
  25. package/lib/ActionList2/Selection.d.ts +5 -0
  26. package/lib/ActionList2/Selection.js +70 -0
  27. package/lib/ActionList2/Visuals.d.ts +9 -0
  28. package/lib/ActionList2/Visuals.js +90 -0
  29. package/lib/ActionList2/index.d.ts +36 -0
  30. package/lib/ActionList2/index.js +47 -0
  31. package/lib/Overlay.js +3 -1
  32. package/lib/Portal/Portal.js +3 -2
  33. package/lib/_TextInputWrapper.js +2 -2
  34. package/lib/__tests__/ActionList2.test.d.ts +1 -0
  35. package/lib/__tests__/ActionList2.test.js +53 -0
  36. package/lib/__tests__/AnchoredOverlay.test.js +4 -2
  37. package/lib/__tests__/TextInputWithTokens.test.js +1 -10
  38. package/lib/__tests__/utils/createSlots.test.d.ts +1 -0
  39. package/lib/__tests__/utils/createSlots.test.js +75 -0
  40. package/lib/hooks/useAnchoredPosition.js +3 -2
  41. package/lib/hooks/useCombinedRefs.d.ts +2 -2
  42. package/lib/hooks/useCombinedRefs.js +4 -6
  43. package/lib/hooks/useResizeObserver.js +2 -2
  44. package/lib/stories/ActionList2.stories.js +907 -0
  45. package/lib/stories/TextInput.stories.js +144 -0
  46. package/lib/stories/Token.stories.js +19 -2
  47. package/lib/sx.d.ts +2 -0
  48. package/lib/sx.js +8 -0
  49. package/lib/theme-preval.js +81 -2
  50. package/lib/unreleased.d.ts +7 -0
  51. package/lib/unreleased.js +18 -0
  52. package/lib/utils/create-slots.d.ts +17 -0
  53. package/lib/utils/create-slots.js +105 -0
  54. package/lib/utils/testing.d.ts +14 -1
  55. package/lib/utils/use-force-update.d.ts +1 -0
  56. package/lib/utils/use-force-update.js +19 -0
  57. package/lib/utils/useIsomorphicLayoutEffect.d.ts +3 -0
  58. package/lib/utils/useIsomorphicLayoutEffect.js +12 -0
  59. package/lib-esm/ActionList2/Description.d.ts +12 -0
  60. package/lib-esm/ActionList2/Description.js +37 -0
  61. package/lib-esm/ActionList2/Divider.d.ts +5 -0
  62. package/lib-esm/ActionList2/Divider.js +23 -0
  63. package/lib-esm/ActionList2/Group.d.ts +11 -0
  64. package/lib-esm/ActionList2/Group.js +40 -0
  65. package/lib-esm/ActionList2/Header.d.ts +26 -0
  66. package/lib-esm/ActionList2/Header.js +44 -0
  67. package/lib-esm/ActionList2/Item.d.ts +63 -0
  68. package/lib-esm/ActionList2/Item.js +210 -0
  69. package/lib-esm/ActionList2/LinkItem.d.ts +17 -0
  70. package/lib-esm/ActionList2/LinkItem.js +43 -0
  71. package/lib-esm/ActionList2/List.d.ts +26 -0
  72. package/lib-esm/ActionList2/List.js +37 -0
  73. package/lib-esm/ActionList2/Selection.d.ts +5 -0
  74. package/lib-esm/ActionList2/Selection.js +52 -0
  75. package/lib-esm/ActionList2/Visuals.d.ts +9 -0
  76. package/lib-esm/ActionList2/Visuals.js +68 -0
  77. package/lib-esm/ActionList2/index.d.ts +36 -0
  78. package/lib-esm/ActionList2/index.js +33 -0
  79. package/lib-esm/Overlay.js +2 -1
  80. package/lib-esm/Portal/Portal.js +2 -1
  81. package/lib-esm/_TextInputWrapper.js +2 -2
  82. package/lib-esm/__tests__/ActionList2.test.d.ts +1 -0
  83. package/lib-esm/__tests__/ActionList2.test.js +41 -0
  84. package/lib-esm/__tests__/AnchoredOverlay.test.js +4 -2
  85. package/lib-esm/__tests__/TextInputWithTokens.test.js +1 -10
  86. package/lib-esm/__tests__/utils/createSlots.test.d.ts +1 -0
  87. package/lib-esm/__tests__/utils/createSlots.test.js +67 -0
  88. package/lib-esm/hooks/useAnchoredPosition.js +2 -1
  89. package/lib-esm/hooks/useCombinedRefs.d.ts +2 -2
  90. package/lib-esm/hooks/useCombinedRefs.js +3 -2
  91. package/lib-esm/hooks/useResizeObserver.js +2 -2
  92. package/lib-esm/stories/ActionList2.stories.js +796 -0
  93. package/lib-esm/stories/TextInput.stories.js +117 -0
  94. package/lib-esm/stories/Token.stories.js +14 -1
  95. package/lib-esm/sx.d.ts +2 -0
  96. package/lib-esm/sx.js +3 -1
  97. package/lib-esm/theme-preval.js +81 -2
  98. package/lib-esm/unreleased.d.ts +7 -0
  99. package/lib-esm/unreleased.js +8 -0
  100. package/lib-esm/utils/create-slots.d.ts +17 -0
  101. package/lib-esm/utils/create-slots.js +84 -0
  102. package/lib-esm/utils/testing.d.ts +14 -1
  103. package/lib-esm/utils/use-force-update.d.ts +1 -0
  104. package/lib-esm/utils/use-force-update.js +6 -0
  105. package/lib-esm/utils/useIsomorphicLayoutEffect.d.ts +3 -0
  106. package/lib-esm/utils/useIsomorphicLayoutEffect.js +3 -0
  107. package/migrating.md +1 -1
  108. package/package-lock.json +146 -7
  109. package/package.json +5 -3
  110. package/script/build +2 -0
  111. package/src/ActionList2/Description.tsx +49 -0
  112. package/src/ActionList2/Divider.tsx +24 -0
  113. package/src/ActionList2/Group.tsx +34 -0
  114. package/src/ActionList2/Header.tsx +58 -0
  115. package/src/ActionList2/Item.tsx +245 -0
  116. package/src/ActionList2/LinkItem.tsx +49 -0
  117. package/src/ActionList2/List.tsx +55 -0
  118. package/src/ActionList2/Selection.tsx +40 -0
  119. package/src/ActionList2/Visuals.tsx +76 -0
  120. package/src/ActionList2/index.ts +39 -0
  121. package/src/Overlay.tsx +2 -1
  122. package/src/Portal/Portal.tsx +2 -1
  123. package/src/_TextInputWrapper.tsx +7 -0
  124. package/src/__tests__/ActionList2.test.tsx +47 -0
  125. package/src/__tests__/AnchoredOverlay.test.tsx +2 -2
  126. package/src/__tests__/TextInputWithTokens.test.tsx +0 -10
  127. package/src/__tests__/__snapshots__/ActionList2.test.tsx.snap +14 -0
  128. package/src/__tests__/__snapshots__/AnchoredOverlay.test.tsx.snap +35 -135
  129. package/src/__tests__/utils/__snapshots__/createSlots.test.tsx.snap +55 -0
  130. package/src/__tests__/utils/createSlots.test.tsx +74 -0
  131. package/src/hooks/useAnchoredPosition.ts +2 -1
  132. package/src/hooks/useCombinedRefs.ts +3 -3
  133. package/src/hooks/useResizeObserver.ts +2 -2
  134. package/src/stories/ActionList2.stories.tsx +1288 -0
  135. package/src/stories/TextInput.stories.tsx +113 -0
  136. package/src/stories/Token.stories.tsx +12 -1
  137. package/src/sx.ts +3 -0
  138. package/src/theme-preval.js +1 -0
  139. package/src/unreleased.ts +9 -0
  140. package/src/utils/create-slots.tsx +96 -0
  141. package/src/utils/use-force-update.ts +7 -0
  142. package/src/utils/useIsomorphicLayoutEffect.ts +10 -0
  143. package/stats.html +1 -1
@@ -0,0 +1,113 @@
1
+ import React, {useState, ReactNode} from 'react'
2
+ import {Meta} from '@storybook/react'
3
+
4
+ import {BaseStyles, Box, ThemeProvider, Text} from '..'
5
+ import TextInput, {TextInputProps} from '../TextInput'
6
+ import {CheckIcon} from '@primer/octicons-react'
7
+
8
+ export default {
9
+ title: 'Forms/Text Input',
10
+ component: TextInput,
11
+ decorators: [
12
+ Story => {
13
+ return (
14
+ <ThemeProvider>
15
+ <BaseStyles>
16
+ <Box paddingTop={5}>{Story()}</Box>
17
+ </BaseStyles>
18
+ </ThemeProvider>
19
+ )
20
+ }
21
+ ],
22
+ argTypes: {
23
+ sx: {
24
+ table: {
25
+ disable: true
26
+ }
27
+ },
28
+ block: {
29
+ name: 'Block',
30
+ defaultValue: false,
31
+ control: {
32
+ type: 'boolean'
33
+ }
34
+ },
35
+ disabled: {
36
+ name: 'Disabled',
37
+ defaultValue: false,
38
+ control: {
39
+ type: 'boolean'
40
+ }
41
+ },
42
+ variant: {
43
+ name: 'Variants',
44
+ options: ['small', 'medium', 'large'],
45
+ control: {type: 'radio'}
46
+ }
47
+ }
48
+ } as Meta
49
+
50
+ const Label = ({htmlFor, children}: {htmlFor: string; children: ReactNode}) => (
51
+ <Text as="label" htmlFor={htmlFor} sx={{fontWeight: 600, fontSize: 14}}>
52
+ {children}
53
+ </Text>
54
+ )
55
+
56
+ export const Default = (args: TextInputProps) => {
57
+ const [value, setValue] = useState('')
58
+
59
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
60
+ setValue(event.target.value)
61
+ }
62
+
63
+ const inputId = 'basic-text-input'
64
+
65
+ return (
66
+ <form>
67
+ <div className="form-group">
68
+ <div className="form-group-header">
69
+ <Label htmlFor={inputId}>Example label</Label>
70
+ </div>
71
+ <div className="form-group-body">
72
+ <TextInput id={inputId} value={value} onChange={handleChange} {...args} />
73
+ </div>
74
+ </div>
75
+ </form>
76
+ )
77
+ }
78
+
79
+ export const WithLeadingIcon = (args: TextInputProps) => {
80
+ const [value, setValue] = useState('')
81
+
82
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
83
+ setValue(event.target.value)
84
+ }
85
+
86
+ const inputId = 'basic-text-input-with-leading-icon'
87
+
88
+ return (
89
+ <form>
90
+ <Label htmlFor={inputId}>Example label</Label>
91
+ <br />
92
+ <TextInput icon={CheckIcon} id={inputId} value={value} onChange={handleChange} type="password" {...args} />
93
+ </form>
94
+ )
95
+ }
96
+
97
+ export const Password = (args: TextInputProps) => {
98
+ const [value, setValue] = useState('')
99
+
100
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
101
+ setValue(event.target.value)
102
+ }
103
+
104
+ const inputId = 'basic-text-input-as-password'
105
+
106
+ return (
107
+ <form>
108
+ <Label htmlFor={inputId}>Password</Label>
109
+ <br />
110
+ <TextInput type="password" id={inputId} value={value} onChange={handleChange} {...args} />
111
+ </form>
112
+ )
113
+ }
@@ -7,6 +7,7 @@ import {BaseStyles, ThemeProvider} from '..'
7
7
  import Box from '../Box'
8
8
  import Token, {TokenProps} from '../Token/Token'
9
9
  import Text from '../Text'
10
+ import {GitBranchIcon} from '@primer/octicons-react'
10
11
 
11
12
  export default {
12
13
  title: 'Tokens/Default',
@@ -29,7 +30,7 @@ export default {
29
30
  ]
30
31
  } as Meta
31
32
 
32
- const excludedControlKeys = ['id', 'as', 'tabIndex', 'onRemove']
33
+ const excludedControlKeys = ['id', 'as', 'tabIndex', 'onRemove', 'leadingVisual']
33
34
 
34
35
  const SingleExampleContainer: React.FC<{label?: string}> = ({children, label}) => (
35
36
  <Box
@@ -94,6 +95,16 @@ export const Interactive = (args: Omit<TokenProps, 'ref' | 'text'>) => {
94
95
  }
95
96
  Interactive.parameters = {controls: {exclude: [...excludedControlKeys, 'hideRemoveButton', 'text']}}
96
97
 
98
+ export const WithLeadingVisual = (args: Omit<TokenProps, 'ref'>) => {
99
+ return (
100
+ <ExampleCollectionContainer>
101
+ <Token {...args} leadingVisual={() => <GitBranchIcon />} />
102
+ </ExampleCollectionContainer>
103
+ )
104
+ }
105
+ WithLeadingVisual.storyName = 'with leadingVisual'
106
+ WithLeadingVisual.parameters = {controls: {exclude: [...excludedControlKeys, 'hideRemoveButton']}}
107
+
97
108
  export const WithOnRemoveFn = (args: Omit<TokenProps, 'ref'>) => {
98
109
  return (
99
110
  <ExampleCollectionContainer>
package/src/sx.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import css, {SystemCssProperties, SystemStyleObject} from '@styled-system/css'
2
2
  import {ThemeColorPaths, ThemeShadowPaths} from './theme'
3
3
  import {ColorProps, ShadowProps} from 'styled-system'
4
+ import merge from 'deepmerge'
4
5
 
5
6
  export type BetterCssProperties = {
6
7
  [K in keyof SystemCssProperties]: K extends keyof ColorProps
@@ -19,3 +20,5 @@ export interface SxProp {
19
20
  const sx = (props: SxProp) => css(props.sx)
20
21
 
21
22
  export default sx
23
+
24
+ export {merge}
@@ -1,5 +1,6 @@
1
1
  // @preval
2
2
  // This file needs to be a JavaScript file using CommonJS to be compatiable with preval
3
+ // Cache bust: 2021-11-04 12:00:00 GMT (This file is cached by our deployment tooling, update this timestamp to rebuild this file)
3
4
 
4
5
  const {default: primitives} = require('@primer/primitives')
5
6
  const {partitionColors, fontStack, omitScale} = require('./utils/theme')
@@ -0,0 +1,9 @@
1
+ /** This is the place where we keep components that are not part of the public
2
+ * api yet (not in main bundle). We don't recommend using it in production.
3
+ *
4
+ * But, they are published on npm and you can import them for experimentation/feedback.
5
+ * example: import {ActionList} from '@primer/components/unreleased
6
+ */
7
+
8
+ // Components
9
+ export * from './ActionList2'
@@ -0,0 +1,96 @@
1
+ import React from 'react'
2
+ import {useForceUpdate} from './use-force-update'
3
+
4
+ /** createSlots is a factory that can create a
5
+ * typesafe Slots + Slot pair to use in a component definition
6
+ * For example: ActionList.Item uses createSlots to get a Slots wrapper
7
+ * + Slot component that is used by LeadingVisual, Description
8
+ */
9
+ const createSlots = <SlotNames extends string>(slotNames: SlotNames[]) => {
10
+ type Slots = {
11
+ [key in SlotNames]?: React.ReactNode
12
+ }
13
+
14
+ type ContextProps = {
15
+ registerSlot: (name: SlotNames, contents: React.ReactNode) => void
16
+ unregisterSlot: (name: SlotNames) => void
17
+ context: Record<string, unknown>
18
+ }
19
+ const SlotsContext = React.createContext<ContextProps>({
20
+ registerSlot: () => null,
21
+ unregisterSlot: () => null,
22
+ context: {}
23
+ })
24
+
25
+ /** Slots uses a Double render strategy inspired by [reach-ui/descendants](https://github.com/reach/reach-ui/tree/develop/packages/descendants)
26
+ * Slot registers themself with the Slots parent.
27
+ * When all the children have mounted = registered themselves in slot,
28
+ * we re-render the parent component to render with slots
29
+ */
30
+ const Slots: React.FC<{
31
+ context?: ContextProps['context']
32
+ children: (slots: Slots) => React.ReactNode
33
+ }> = ({context = {}, children}) => {
34
+ // initialise slots
35
+ const slotsDefinition: Slots = {}
36
+ slotNames.map(name => (slotsDefinition[name] = null))
37
+ const slotsRef = React.useRef<Slots>(slotsDefinition)
38
+
39
+ const rerenderWithSlots = useForceUpdate()
40
+ const [isMounted, setIsMounted] = React.useState(false)
41
+
42
+ // fires after all the effects in children
43
+ React.useEffect(() => {
44
+ rerenderWithSlots()
45
+ setIsMounted(true)
46
+ }, [rerenderWithSlots])
47
+
48
+ const registerSlot = React.useCallback(
49
+ (name: SlotNames, contents: React.ReactNode) => {
50
+ slotsRef.current[name] = contents
51
+
52
+ // don't render until the component mounts = all slots are registered
53
+ if (isMounted) rerenderWithSlots()
54
+ },
55
+ [isMounted, rerenderWithSlots]
56
+ )
57
+
58
+ // Slot can be removed from the tree as well,
59
+ // we need to unregister them from the slot
60
+ const unregisterSlot = React.useCallback(
61
+ (name: SlotNames) => {
62
+ slotsRef.current[name] = null
63
+ rerenderWithSlots()
64
+ },
65
+ [rerenderWithSlots]
66
+ )
67
+
68
+ /**
69
+ * Slots uses a render prop API so abstract the
70
+ * implementation detail of using a context provider.
71
+ */
72
+ const slots = slotsRef.current
73
+
74
+ return (
75
+ <SlotsContext.Provider value={{registerSlot, unregisterSlot, context}}>{children(slots)}</SlotsContext.Provider>
76
+ )
77
+ }
78
+
79
+ const Slot: React.FC<{
80
+ name: SlotNames
81
+ children: React.ReactNode
82
+ }> = ({name, children}) => {
83
+ const {registerSlot, unregisterSlot, context} = React.useContext(SlotsContext)
84
+
85
+ React.useEffect(() => {
86
+ registerSlot(name, typeof children === 'function' ? children(context) : children)
87
+ return () => unregisterSlot(name)
88
+ }, [name, children, registerSlot, unregisterSlot, context])
89
+
90
+ return null
91
+ }
92
+
93
+ return {Slots, Slot}
94
+ }
95
+
96
+ export default createSlots
@@ -0,0 +1,7 @@
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
+
4
+ export const useForceUpdate = () => {
5
+ const [, rerender] = React.useState({})
6
+ return React.useCallback(() => rerender({}), [])
7
+ }
@@ -0,0 +1,10 @@
1
+ import {useEffect, useLayoutEffect} from 'react'
2
+
3
+ const useIsomorphicLayoutEffect =
4
+ typeof window !== 'undefined' &&
5
+ typeof window.document !== 'undefined' &&
6
+ typeof window.document.createElement !== 'undefined'
7
+ ? useLayoutEffect
8
+ : useEffect
9
+
10
+ export default useIsomorphicLayoutEffect