-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from szhsin/feat/auto-height
feat: auto height
- Loading branch information
Showing
13 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState, useCallback, useLayoutEffect as useLayoutEffect$1, useEffect } from 'react'; | ||
|
||
const useLayoutEffect = typeof window !== 'undefined' && window.document && window.document.createElement ? useLayoutEffect$1 : useEffect; | ||
const findOverflowAncestor = element => { | ||
while (element) { | ||
element = element.parentElement; | ||
if (!element || element === document.body) return; | ||
const { | ||
overflow, | ||
overflowX, | ||
overflowY | ||
} = getComputedStyle(element); | ||
if (/auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX)) return element; | ||
} | ||
}; | ||
const useAutoHeight = ({ | ||
anchorRef, | ||
show, | ||
margin = 0 | ||
}) => { | ||
const [height, setHeight] = useState(); | ||
const computeHeight = useCallback(() => { | ||
const anchor = anchorRef.current; | ||
if (!anchor) return; | ||
const overflowAncestor = findOverflowAncestor(anchor); | ||
const bottomBoundary = overflowAncestor ? overflowAncestor.getBoundingClientRect().bottom : window.innerHeight; | ||
const newHeight = bottomBoundary - anchor.getBoundingClientRect().bottom - margin; | ||
setHeight(Math.max(newHeight, 0)); | ||
}, [anchorRef, margin]); | ||
useLayoutEffect(() => { | ||
show && computeHeight(); | ||
}, [show, computeHeight]); | ||
return [height, computeHeight]; | ||
}; | ||
|
||
export { useAutoHeight }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState, useCallback, useEffect, useLayoutEffect as _useLayoutEffect } from 'react'; | ||
|
||
const useLayoutEffect = | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
typeof window !== 'undefined' && window.document && window.document.createElement! | ||
? _useLayoutEffect | ||
: useEffect; | ||
|
||
const findOverflowAncestor = (element: Element | null) => { | ||
while (element) { | ||
element = element.parentElement; | ||
if (!element || element === document.body) return; | ||
const { overflow, overflowX, overflowY } = getComputedStyle(element); | ||
if (/auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX)) return element; | ||
} | ||
}; | ||
|
||
const useAutoHeight = ({ | ||
anchorRef, | ||
show, | ||
margin = 0 | ||
}: { | ||
anchorRef: React.RefObject<Element>; | ||
show?: boolean; | ||
margin?: number; | ||
}) => { | ||
const [height, setHeight] = useState<React.CSSProperties['maxHeight']>(); | ||
|
||
const computeHeight = useCallback(() => { | ||
const anchor = anchorRef.current; | ||
if (!anchor) return; | ||
const overflowAncestor = findOverflowAncestor(anchor); | ||
const bottomBoundary = overflowAncestor | ||
? overflowAncestor.getBoundingClientRect().bottom | ||
: window.innerHeight; | ||
const newHeight = bottomBoundary - anchor.getBoundingClientRect().bottom - margin; | ||
setHeight(Math.max(newHeight, 0)); | ||
}, [anchorRef, margin]); | ||
|
||
useLayoutEffect(() => { | ||
show && computeHeight(); | ||
}, [show, computeHeight]); | ||
|
||
return [height, computeHeight] as const; | ||
}; | ||
|
||
export { useAutoHeight }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare const useAutoHeight: ({ anchorRef, show, margin }: { | ||
anchorRef: React.RefObject<Element>; | ||
show?: boolean | undefined; | ||
margin?: number | undefined; | ||
}) => readonly [import("csstype").Property.MaxHeight<string | number> | undefined, () => void]; | ||
export { useAutoHeight }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/// <reference types="react" /> | ||
import type { GetProps, AutocompleteProps } from '../common'; | ||
import type { InputHTMLAttributes } from 'react'; | ||
import type { GetProps, AutocompleteProps, PropsWithObjectRef } from '../common'; | ||
declare const useAutocomplete: <T, FeatureActions>({ onChange, isItemDisabled, feature: useFeature, traversal: useTraversal, getItemValue: _getItemValue }: AutocompleteProps<T, FeatureActions>) => { | ||
setInputValue: (value: string) => void; | ||
focusItem: T | null | undefined; | ||
setFocusItem: (item?: T | null | undefined) => void; | ||
open: boolean; | ||
setOpen: (value: boolean) => void; | ||
getInputProps: () => import("react").InputHTMLAttributes<HTMLInputElement>; | ||
getInputProps: () => PropsWithObjectRef<InputHTMLAttributes<HTMLInputElement>>; | ||
getListProps: () => import("react").HTMLAttributes<HTMLElement>; | ||
} & Omit<GetProps<T> & FeatureActions, "getInputProps" | "getListProps">; | ||
} & Omit<GetProps<T> & FeatureActions, "getListProps" | "getInputProps">; | ||
export { useAutocomplete }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters