Skip to content

Commit 7e09396

Browse files
committed
Fixed drops to be connected to the dom instantly.
1 parent 60c6d06 commit 7e09396

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

src/components/drops/drop/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, useEffect } from "react"
1+
import React, { forwardRef, useLayoutEffect } from "react"
22
import ReactDOM from "react-dom"
33
import useDropElement from "src/hooks/use-drop-element"
44
import useKeyboardEsc from "src/hooks/use-keyboard-esc"
@@ -48,9 +48,8 @@ const Drop = forwardRef(
4848
keepHorizontal
4949
)
5050

51-
useEffect(() => {
52-
const id = requestAnimationFrame(updatePosition)
53-
return () => cancelAnimationFrame(id)
51+
useLayoutEffect(() => {
52+
updatePosition()
5453
}, [updatePosition])
5554

5655
useDimensionChange(target, updatePosition)

src/components/drops/menu/dropdown.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Container = styled(Flex)`
1212
list-style-type: none;
1313
`
1414

15+
const defaultEstimateSize = () => 28
16+
1517
const Dropdown = ({
1618
hideShadow,
1719
itemProps,
@@ -22,6 +24,8 @@ const Dropdown = ({
2224
hasSearch,
2325
searchMargin = [4],
2426
gap = 0,
27+
width = "300px",
28+
estimateSize = defaultEstimateSize,
2529
close,
2630
...rest
2731
}) => {
@@ -52,7 +56,7 @@ const Dropdown = ({
5256
scrollOffsetFn: event => (event ? event.target.scrollTop - ref.current.offsetTop : 0),
5357
overscan: 3,
5458
enableSmoothScroll: false,
55-
estimateSize: () => 30,
59+
estimateSize,
5660
})
5761

5862
return (
@@ -81,10 +85,9 @@ const Dropdown = ({
8185
<div
8286
ref={ref}
8387
style={{
84-
minHeight: "100%",
85-
width: "100%",
88+
height: "100%",
8689
overflow: "auto",
87-
minWidth: "300px",
90+
width,
8891
}}
8992
>
9093
<div
@@ -94,32 +97,31 @@ const Dropdown = ({
9497
position: "relative",
9598
}}
9699
>
97-
{ref.current?.isConnected &&
98-
rowVirtualizer.getVirtualItems().map(virtualRow => (
99-
<div
100-
key={virtualRow.key}
101-
style={{
102-
position: "absolute",
103-
top: 0,
104-
left: 0,
105-
width: "100%",
106-
transform: `translateY(${virtualRow.start}px)`,
107-
padding: gap * 2,
108-
overflow: "hidden",
109-
}}
110-
data-index={virtualRow.index}
111-
ref={rowVirtualizer.measureElement}
112-
>
113-
<Item
114-
item={filteredItems[virtualRow.index]}
115-
index={virtualRow.index}
116-
itemProps={itemProps}
117-
value={value}
118-
onItemClick={onItemClick}
119-
close={close}
120-
/>
121-
</div>
122-
))}
100+
{rowVirtualizer.getVirtualItems().map(virtualRow => (
101+
<div
102+
key={virtualRow.key}
103+
style={{
104+
position: "absolute",
105+
top: 0,
106+
left: 0,
107+
width: "100%",
108+
transform: `translateY(${virtualRow.start}px)`,
109+
padding: gap * 2,
110+
overflow: "hidden",
111+
}}
112+
data-index={virtualRow.index}
113+
ref={rowVirtualizer.measureElement}
114+
>
115+
<Item
116+
item={filteredItems[virtualRow.index]}
117+
index={virtualRow.index}
118+
itemProps={itemProps}
119+
value={value}
120+
onItemClick={onItemClick}
121+
close={close}
122+
/>
123+
</div>
124+
))}
123125
</div>
124126
</div>
125127
</Container>

src/components/drops/menu/dropdownItem.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { useLayoutEffect, useRef } from "react"
1+
import React from "react"
22
import styled from "styled-components"
33
import { getColor } from "src/theme"
44
import Flex from "src/components/templates/flex"
5-
import { Text } from "src/components/typography"
5+
import { TextSmall } from "src/components/typography"
66

77
export const ItemContainer = styled(Flex).attrs({
88
as: "li",
99
role: "option",
10-
padding: [2, 4],
10+
padding: [1, 4],
1111
})`
1212
cursor: ${({ cursor }) => cursor ?? "pointer"};
1313
opacity: ${({ disabled }) => (disabled ? 0.4 : 1)};
@@ -24,7 +24,6 @@ const DropdownItem = ({
2424
value: selectedValue,
2525
onItemClick,
2626
index,
27-
measureElement,
2827
style,
2928
...rest
3029
}) => {
@@ -37,7 +36,6 @@ const DropdownItem = ({
3736

3837
return (
3938
<ItemContainer
40-
ref={measureElement}
4139
data-index={index}
4240
aria-selected={selected}
4341
disabled={disabled || selected}
@@ -46,9 +44,9 @@ const DropdownItem = ({
4644
{...rest}
4745
style={style}
4846
>
49-
{reverse && <Text>{label}</Text>}
47+
{reverse && <TextSmall>{label}</TextSmall>}
5048
{icon}
51-
{!reverse && <Text>{label}</Text>}
49+
{!reverse && <TextSmall>{label}</TextSmall>}
5250
</ItemContainer>
5351
)
5452
}

src/components/drops/menu/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ const Menu = forwardRef(
7575

7676
return (
7777
<Fragment>
78-
{clonedChildren}
79-
{!clonedChildren && (
78+
{clonedChildren || (
8079
<MenuButton
8180
ref={setRef}
8281
icon={icon}

src/hooks/use-drop-element/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { useEffect, useMemo } from "react"
1+
import { useLayoutEffect, useMemo } from "react"
22

33
export default () => {
4-
const el = useMemo(() => document.createElement("div"), [])
4+
const el = useMemo(() => {
5+
const div = document.createElement("div")
6+
document.body.append(div)
7+
return div
8+
}, [])
59

6-
useEffect(() => {
7-
document.body.append(el)
10+
useLayoutEffect(() => {
811
return () => document.body.removeChild(el)
912
}, [])
1013

0 commit comments

Comments
 (0)