Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown & Popover fixes for dimensions and scrolling #608

Merged
merged 9 commits into from
Oct 1, 2019
36 changes: 22 additions & 14 deletions src/components/DropDown/DropDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,27 @@ const DropDown = React.memo(function DropDown({
const { width: vw } = useViewport()

const [widthNoPx = MIN_WIDTH] = (width || '').split('px')
const [placeholderMinWidth, setPlaceholderMinWidth] = useState(
Math.min(widthNoPx, MIN_WIDTH)
)
const [buttonWidth, setButtonWidth] = useState(0)
const [getContentWidth, setGetContentWidth] = useState(true)
AquiGorka marked this conversation as resolved.
Show resolved Hide resolved

// Adjust the button width if the item widths are larger than declared width
const popoverRefCallback = useCallback(el => {
if (!el) {
return
const [ghostElement, setGhostElement] = useState(null)
const popoverRefCallback = useCallback(
el => {
if (!el) {
return
}
setGhostElement(el)
},
[setGhostElement]
AquiGorka marked this conversation as resolved.
Show resolved Hide resolved
)
const placeholderMinWidth = useMemo(() => {
if (!ghostElement) {
return Math.min(widthNoPx, MIN_WIDTH)
}
// Add 4 GU to accomodate for caret spacing
setPlaceholderMinWidth(el.clientWidth + 4 * GU)
setGetContentWidth(false)
}, [])
// Re-adjust if the width or items ever change
return ghostElement.clientWidth
}, [ghostElement, widthNoPx])

useEffect(() => {
setGetContentWidth(true)
}, [vw, items])
Expand Down Expand Up @@ -187,7 +192,7 @@ const DropDown = React.memo(function DropDown({
padding-left: ${2 * GU}px;
padding-right: ${1.5 * GU}px;
width: ${width || (wide ? '100%' : 'auto')};
min-width: ${placeholderMinWidth}px;
min-width: ${wide ? 'auto' : `${placeholderMinWidth}px`};
background: ${disabled ? theme.disabled : theme.surface};
color: ${disabled ? theme.disabledContent : theme.surfaceContent};
border: ${disabled ? 0 : 1}px solid
Expand All @@ -203,7 +208,9 @@ const DropDown = React.memo(function DropDown({
`}
{...props}
>
<Label selectedIndex={selectedIndex} selectedLabel={selectedLabel} />
<div css="overflow: hidden;">
<Label selectedIndex={selectedIndex} selectedLabel={selectedLabel} />
</div>
<IconDown
size="tiny"
css={`
Expand Down Expand Up @@ -280,7 +287,6 @@ const PopoverContent = React.memo(function PopoverContent({
const theme = useTheme()
return (
<div
ref={refCallback}
css={`
min-width: ${buttonWidth}px;
color: ${theme.surfaceContentSecondary};
Expand All @@ -298,8 +304,10 @@ const PopoverContent = React.memo(function PopoverContent({
</div>
)}
<ul
ref={refCallback}
css={`
margin: 0;
padding: 0;
bpierre marked this conversation as resolved.
Show resolved Hide resolved
list-style: none;
width: 100%;
`}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import Popper from 'popper.js'
import { Transition, animated } from 'react-spring'
import { useRoot } from '../../providers'
import { springs, RADIUS } from '../../style'
import { springs, GU, RADIUS } from '../../style'
import { useTheme } from '../../theme'
import { noop, stylingProps, warn, KEY_ESC } from '../../utils'
import RootPortal from '../RootPortal/RootPortal'
Expand Down Expand Up @@ -193,6 +193,9 @@ class PopoverBase extends React.Component {
/* Having the popover visible already means that it focused. */
outline: 0;
}
max-height: calc(100vh - ${2 * GU}px);
max-width: calc(100vw - ${2 * GU}px);
AquiGorka marked this conversation as resolved.
Show resolved Hide resolved
overflow-y: auto;
`}
{...stylingProps(this)}
>
Expand Down