Skip to content

Commit bbf171b

Browse files
committed
Add optional elementRef prop to TransitionComponents
1 parent da09b86 commit bbf171b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/components/transition/Slider.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
1+
import { useCallback, useEffect, useState } from 'preact/hooks';
22

3+
import { useSyncedRef } from '../../hooks/use-synced-ref';
34
import type { TransitionComponent } from '../../types';
5+
import { downcastRef } from '../../util/typing';
46

57
const Slider: TransitionComponent = ({
68
children,
79
direction = 'in',
810
onTransitionEnd,
911
delay,
12+
elementRef,
1013
}) => {
1114
const visible = direction === 'in';
12-
const containerRef = useRef<HTMLDivElement | null>(null);
15+
const containerRef = useSyncedRef(elementRef);
1316
const [containerHeight, setContainerHeight] = useState(visible ? 'auto' : 0);
1417

1518
// Whether the content is currently partially or wholly visible. This is
@@ -52,7 +55,7 @@ const Slider: TransitionComponent = ({
5255

5356
setContainerHeight(0);
5457
}
55-
}, [containerHeight, visible]);
58+
}, [containerHeight, containerRef, visible]);
5659

5760
const handleTransitionEnd = useCallback(
5861
(e: TransitionEvent) => {
@@ -72,7 +75,7 @@ const Slider: TransitionComponent = ({
7275
}
7376
onTransitionEnd?.(direction);
7477
},
75-
[setContainerHeight, visible, onTransitionEnd, direction],
78+
[containerRef, visible, onTransitionEnd, direction],
7679
);
7780

7881
const isFullyVisible = containerHeight === 'auto';
@@ -84,7 +87,7 @@ const Slider: TransitionComponent = ({
8487
//
8588
// @ts-ignore
8689
ontransitionend={handleTransitionEnd}
87-
ref={containerRef}
90+
ref={downcastRef(containerRef)}
8891
style={{
8992
display: contentVisible ? '' : 'none',
9093
height: containerHeight,

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ export type IconComponent = FunctionComponent<JSX.SVGAttributes<SVGSVGElement>>;
5757
export type TransitionComponent = FunctionComponent<{
5858
/** Whether the children should be revealed ("in") or hidden ("out"). */
5959
direction?: 'in' | 'out';
60-
6160
/** Callback invoked when transition ends. */
6261
onTransitionEnd?: (direction: 'in' | 'out') => void;
62+
/** Ref associated with component's outermost or primary element */
63+
elementRef?: Ref<HTMLElement | undefined>;
6364

6465
/**
6566
* Delay before transitions begin. This corresponds to the `transition-delay`

0 commit comments

Comments
 (0)