Skip to content

Commit

Permalink
Updated dependencies and fixed select list
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 7, 2023
1 parent 2a68b63 commit 1abf651
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 134 deletions.
10 changes: 8 additions & 2 deletions components/patterns/elements/select-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
RefObject,
useEffect,
useId,
useLayoutEffect,
useRef,
useState
} from "react";
import {ChevronDownIcon} from "@heroicons/react/20/solid";
import OutsideClickHandler from "@/components/utils/outside-click-handler";
import {useIsClient} from "usehooks-ts";

interface Props {
options: SelectOptionDefinition<string>[];
Expand Down Expand Up @@ -54,7 +56,7 @@ const renderSelectedValue = (value: SelectValue<string, boolean>, options: Selec
function CustomOption(props: OptionProps) {
const {children, value, rootRef, disabled = false} = props;
const {getRootProps, highlighted, selected} = useOption({rootRef: rootRef, value, disabled, label: children});
const {id, ...otherProps} = getRootProps();
const {id, ...otherProps}: { id: string } = getRootProps();
const selectedStyles = "su-bg-archway su-text-white " + (highlighted ? "su-underline" : "")
const highlightedStyles = "su-bg-black-10 su-text-black su-underline"

Expand Down Expand Up @@ -94,6 +96,7 @@ const SelectList = ({options, label, multiple, ariaLabelledby, ...props}: Props)
const labeledBy = ariaLabelledby ?? labelId;
const listboxRef = useRef<HTMLUListElement>(null);
const [listboxVisible, setListboxVisible] = useState(false);
const isClient = useIsClient();

const {getButtonProps, getListboxProps, contextValue, value} = useSelect<string, boolean>({
listboxRef,
Expand All @@ -107,7 +110,7 @@ const SelectList = ({options, label, multiple, ariaLabelledby, ...props}: Props)
if (listboxVisible) listboxRef.current?.focus();
}, [listboxVisible]);

useEffect(() => {
useLayoutEffect(() => {
const parentContainer = listboxRef.current?.parentElement?.getBoundingClientRect();
if (parentContainer && (parentContainer.bottom > window.innerHeight || parentContainer.top < 0)) {
listboxRef.current?.parentElement?.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
Expand All @@ -119,6 +122,9 @@ const SelectList = ({options, label, multiple, ariaLabelledby, ...props}: Props)
const onClickOutside = () => {
setListboxVisible(false)
}

if (!isClient) return null;

return (
<OutsideClickHandler
onClickOutside={onClickOutside}
Expand Down
16 changes: 8 additions & 8 deletions lib/hooks/useIsCentered.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {MutableRefObject, useLayoutEffect, useState} from "react";
import {useDebouncedCallback} from "use-debounce";
import {MutableRefObject, useCallback, useLayoutEffect, useState} from "react";
import {useDebounce} from "usehooks-ts";

const useIsCentered = (ref: MutableRefObject<any>) => {
const [isCentered, setIsCentered] = useState(false)
const [isCentered, setIsCentered] = useState<boolean>(false)
const debouncedIsCentered = useDebounce<boolean>(isCentered);

const resizeHandler = useDebouncedCallback(() => {
const resizeHandler = useCallback(() => {
if (ref?.current) {
const boundingBox = ref.current.getBoundingClientRect();
setIsCentered(Math.abs((window.innerWidth - boundingBox.width) / 2 - boundingBox.x) <= 10);
}
}, 200)

}, [ref]);

useLayoutEffect(() => {
resizeHandler();
window.addEventListener('resize', resizeHandler);
return () => window.removeEventListener('resize', resizeHandler);
}, [ref])
}, [ref, resizeHandler])

return isCentered;
return debouncedIsCentered;
}

export default useIsCentered;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"drupal-jsonapi-params": "^2.0.0",
"html-react-parser": "^5.0.6",
"jsona": "^1.11.0",
"next": "^13.3.0",
"next": "^14.0.1",
"next-drupal": "^1.2.1",
"nextjs-google-analytics": "^2.3.3",
"react": "^18.2.0",
Expand All @@ -40,15 +40,15 @@
"server-only": "^0.0.1",
"sharp": "^0.32.0",
"tailwind-merge": "^2.0.0",
"use-debounce": "^9.0.4",
"usehooks-ts": "^2.9.1",
"xml2js": "^0.6.0"
},
"devDependencies": {
"@types/node": "^20.1.1",
"@types/react": "^18.0.21",
"autoprefixer": "^10.4.4",
"eslint": "^8.12.0",
"eslint-config-next": "^13.0.0",
"eslint-config-next": "^14.0.1",
"postcss": "^8.4.12",
"tailwindcss": "^3.0.23",
"typescript": "^5.0.4"
Expand Down
Loading

0 comments on commit 1abf651

Please sign in to comment.