Skip to content

Commit cf9c66a

Browse files
committed
revert #2204
1 parent 4badb57 commit cf9c66a

File tree

15 files changed

+133
-27135
lines changed

15 files changed

+133
-27135
lines changed

docs/package-lock.json

Lines changed: 16 additions & 27074 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Autocomplete/AutocompleteInput.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {
22
ChangeEventHandler,
33
FocusEventHandler,
44
KeyboardEventHandler,
5+
MutableRefObject,
56
useCallback,
67
useContext,
78
useEffect,
@@ -10,7 +11,7 @@ import React, {
1011
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '@radix-ui/react-polymorphic'
1112
import {AutocompleteContext} from './AutocompleteContext'
1213
import TextInput from '../TextInput'
13-
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
14+
import {useCombinedRefs} from '../hooks/useCombinedRefs'
1415
import {ComponentProps} from '../utils/types'
1516

1617
type InternalAutocompleteInputProps = {
@@ -38,7 +39,7 @@ const AutocompleteInput = React.forwardRef(
3839
setShowMenu,
3940
showMenu
4041
} = autocompleteContext
41-
useRefObjectAsForwardedRef(forwardedRef, inputRef)
42+
const combinedInputRef = useCombinedRefs(inputRef, forwardedRef)
4243
const [highlightRemainingText, setHighlightRemainingText] = useState<boolean>(true)
4344

4445
const handleInputFocus: FocusEventHandler<HTMLInputElement> = useCallback(
@@ -57,12 +58,12 @@ const AutocompleteInput = React.forwardRef(
5758
// this prevents the menu from hiding when the user is clicking an option in the Autoselect.Menu,
5859
// but still hides the menu when the user blurs the input by tabbing out or clicking somewhere else on the page
5960
setTimeout(() => {
60-
if (document.activeElement !== inputRef.current) {
61+
if (document.activeElement !== combinedInputRef.current) {
6162
setShowMenu(false)
6263
}
6364
}, 0)
6465
},
65-
[onBlur, setShowMenu, inputRef]
66+
[onBlur, setShowMenu, combinedInputRef]
6667
)
6768

6869
const handleInputChange: ChangeEventHandler<HTMLInputElement> = useCallback(
@@ -156,7 +157,7 @@ const AutocompleteInput = React.forwardRef(
156157
onKeyDown={handleInputKeyDown}
157158
onKeyPress={onInputKeyPress}
158159
onKeyUp={handleInputKeyUp}
159-
ref={inputRef}
160+
ref={combinedInputRef as MutableRefObject<HTMLInputElement>}
160161
aria-controls={`${id}-listbox`}
161162
aria-autocomplete="both"
162163
role="combobox"

src/Autocomplete/AutocompleteOverlay.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {useAnchoredPosition} from '../hooks'
33
import Overlay, {OverlayProps} from '../Overlay'
44
import {ComponentProps} from '../utils/types'
55
import {AutocompleteContext} from './AutocompleteContext'
6-
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
6+
import {useCombinedRefs} from '../hooks/useCombinedRefs'
77

88
type AutocompleteOverlayInternalProps = {
99
/**
@@ -39,7 +39,7 @@ function AutocompleteOverlay({
3939
[showMenu, selectedItemLength]
4040
)
4141

42-
useRefObjectAsForwardedRef(scrollContainerRef, floatingElementRef)
42+
const combinedOverlayRef = useCombinedRefs(scrollContainerRef, floatingElementRef)
4343

4444
const closeOptionList = useCallback(() => {
4545
setShowMenu(false)
@@ -55,7 +55,7 @@ function AutocompleteOverlay({
5555
preventFocusOnOpen={true}
5656
onClickOutside={closeOptionList}
5757
onEscape={closeOptionList}
58-
ref={floatingElementRef as React.RefObject<HTMLDivElement>}
58+
ref={combinedOverlayRef as React.RefObject<HTMLDivElement>}
5959
top={position?.top}
6060
left={position?.left}
6161
visibility={showMenu ? 'visible' : 'hidden'}

src/Details/Details.stories.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
import {Meta} from '@storybook/react'
3+
import Details from './'
4+
import useDetails from '../hooks/useDetails'
5+
6+
export default {
7+
title: 'Details',
8+
component: Details
9+
} as Meta
10+
11+
export const Default = () => {
12+
const {getDetailsProps} = useDetails({closeOnOutsideClick: true})
13+
14+
return <Details {...getDetailsProps()}>This is some content</Details>
15+
}

src/Details.tsx renamed to src/Details/Details.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import styled from 'styled-components'
2-
import sx, {SxProp} from './sx'
3-
import {ComponentProps} from './utils/types'
2+
import sx, {SxProp} from '../sx'
3+
import {ComponentProps} from '../utils/types'
4+
import {DetailsProps as GetDetailsProps} from '../hooks/useDetails'
45

5-
const Details = styled.details<SxProp>`
6+
const Details = styled.details<SxProp & GetDetailsProps>`
67
& > summary {
78
list-style: none;
89
}

src/Details/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Details from './Details'
2+
export default Details
3+
4+
export * from './Details'

src/Dialog.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import useDialog from './hooks/useDialog'
77
import sx, {SxProp} from './sx'
88
import Text from './Text'
99
import {ComponentProps} from './utils/types'
10-
import {useRefObjectAsForwardedRef} from './hooks/useRefObjectAsForwardedRef'
10+
import {useCombinedRefs} from './hooks/useCombinedRefs'
1111

1212
const noop = () => null
1313

@@ -95,8 +95,7 @@ type InternalDialogProps = {
9595
const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>(
9696
({children, onDismiss = noop, isOpen, initialFocusRef, returnFocusRef, ...props}, forwardedRef) => {
9797
const overlayRef = useRef(null)
98-
const modalRef = useRef<HTMLDivElement>(null)
99-
useRefObjectAsForwardedRef(forwardedRef, modalRef)
98+
const modalRef = useCombinedRefs(forwardedRef)
10099
const closeButtonRef = useRef(null)
101100

102101
const onCloseClick = () => {

src/Dialog/Dialog.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {XIcon} from '@primer/octicons-react'
1111
import {useFocusZone} from '../hooks/useFocusZone'
1212
import {FocusKeys} from '@primer/behaviors'
1313
import Portal from '../Portal'
14-
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
14+
import {useCombinedRefs} from '../hooks/useCombinedRefs'
1515
import {useSSRSafeId} from '@react-aria/ssr'
1616

1717
const ANIMATION_DURATION = '200ms'
@@ -274,7 +274,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
274274
const defaultedProps = {...props, title, subtitle, role, dialogLabelId, dialogDescriptionId}
275275

276276
const dialogRef = useRef<HTMLDivElement>(null)
277-
useRefObjectAsForwardedRef(forwardedRef, dialogRef)
277+
const combinedRef = useCombinedRefs(dialogRef, forwardedRef)
278278
const backdropRef = useRef<HTMLDivElement>(null)
279279
useFocusTrap({containerRef: dialogRef, restoreFocusOnCleanUp: true, initialFocusRef: autoFocusedFooterButtonRef})
280280

@@ -297,7 +297,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
297297
<StyledDialog
298298
width={width}
299299
height={height}
300-
ref={dialogRef}
300+
ref={combinedRef}
301301
role={role}
302302
aria-labelledby={dialogLabelId}
303303
aria-describedby={dialogDescriptionId}

src/Overlay.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {AriaRole, Merge} from './utils/types'
66
import {useOverlay, TouchOrMouseEvent} from './hooks'
77
import Portal from './Portal'
88
import sx, {SxProp} from './sx'
9-
import {useRefObjectAsForwardedRef} from './hooks/useRefObjectAsForwardedRef'
9+
import {useCombinedRefs} from './hooks/useCombinedRefs'
1010
import type {AnchorSide} from '@primer/behaviors'
1111
import {useTheme} from './ThemeProvider'
1212
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '@radix-ui/react-polymorphic'
@@ -142,7 +142,7 @@ const Overlay = React.forwardRef<HTMLDivElement, OwnOverlayProps>(
142142
forwardedRef
143143
): ReactElement => {
144144
const overlayRef = useRef<HTMLDivElement>(null)
145-
useRefObjectAsForwardedRef(forwardedRef, overlayRef)
145+
const combinedRef = useCombinedRefs(overlayRef, forwardedRef)
146146
const {theme} = useTheme()
147147
const slideAnimationDistance = parseInt(get('space.2')(theme).replace('px', ''))
148148
const slideAnimationEasing = get('animation.easeOutCubic')(theme)
@@ -158,10 +158,10 @@ const Overlay = React.forwardRef<HTMLDivElement, OwnOverlayProps>(
158158
})
159159

160160
useEffect(() => {
161-
if (height === 'initial' && overlayRef.current?.clientHeight) {
162-
overlayRef.current.style.height = `${overlayRef.current.clientHeight}px`
161+
if (height === 'initial' && combinedRef.current?.clientHeight) {
162+
combinedRef.current.style.height = `${combinedRef.current.clientHeight}px`
163163
}
164-
}, [height])
164+
}, [height, combinedRef])
165165

166166
useLayoutEffect(() => {
167167
const {x, y} = getSlideAnimationStartingVector(anchorSide)
@@ -185,7 +185,7 @@ const Overlay = React.forwardRef<HTMLDivElement, OwnOverlayProps>(
185185
height={height}
186186
role={role}
187187
{...rest}
188-
ref={overlayRef}
188+
ref={combinedRef}
189189
style={
190190
{
191191
top: `${top || 0}px`,

src/TextInputWithTokens.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {isFocusable} from '@primer/behaviors/utils'
33
import {omit} from '@styled-system/props'
44
import React, {FocusEventHandler, KeyboardEventHandler, MouseEventHandler, RefObject, useRef, useState} from 'react'
55
import Box from './Box'
6-
import {useRefObjectAsForwardedRef} from './hooks/useRefObjectAsForwardedRef'
6+
import {useProvidedRefOrCreate} from './hooks'
7+
import {useCombinedRefs} from './hooks/useCombinedRefs'
78
import {useFocusZone} from './hooks/useFocusZone'
89
import Text from './Text'
910
import {TextInputProps} from './TextInput'
@@ -92,11 +93,12 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
9293
visibleTokenCount,
9394
...rest
9495
}: TextInputWithTokensProps<TokenComponentType>,
95-
forwardedRef: React.ForwardedRef<HTMLInputElement>
96+
externalRef: React.ForwardedRef<HTMLInputElement>
9697
) {
9798
const {onBlur, onFocus, onKeyDown, ...inputPropsRest} = omit(rest)
98-
const ref = useRef<HTMLInputElement>(null)
99-
useRefObjectAsForwardedRef(forwardedRef, ref)
99+
const ref = useProvidedRefOrCreate<HTMLInputElement>(externalRef as React.RefObject<HTMLInputElement>)
100+
const localInputRef = useRef<HTMLInputElement>(null)
101+
const combinedInputRef = useCombinedRefs(localInputRef, ref)
100102
const [selectedTokenIndex, setSelectedTokenIndex] = useState<number | undefined>()
101103
const [tokensAreTruncated, setTokensAreTruncated] = useState<boolean>(Boolean(visibleTokenCount))
102104
const {containerRef} = useFocusZone(
@@ -122,7 +124,7 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
122124
}
123125

124126
if (nextIndex > tokens.length || nextIndex < 1) {
125-
return ref.current || undefined
127+
return combinedInputRef.current || undefined
126128
}
127129

128130
return containerRef.current?.children[nextIndex] as HTMLElement
@@ -228,7 +230,7 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
228230
}
229231

230232
const focusInput: MouseEventHandler = () => {
231-
ref.current?.focus()
233+
combinedInputRef.current?.focus()
232234
}
233235

234236
const preventTokenClickPropagation: MouseEventHandler = event => {
@@ -321,7 +323,7 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
321323
}}
322324
>
323325
<UnstyledTextInput
324-
ref={ref}
326+
ref={combinedInputRef}
325327
disabled={disabled}
326328
onFocus={handleInputFocus}
327329
onBlur={handleInputBlur}

0 commit comments

Comments
 (0)