Fixed initial focus when the modal opens to the first enabled sticker button.

This commit is contained in:
Lucid Kobold
2022-04-09 17:42:20 -05:00
parent 29f8e7138c
commit ad32dd30a1
2 changed files with 13 additions and 3 deletions

View File

@@ -13,7 +13,7 @@ import {
SimpleGrid, SimpleGrid,
Box Box
} from "@chakra-ui/react"; } from "@chakra-ui/react";
import React, { useState, useContext } from "react"; import React, { useState, useContext, useRef } from "react";
import { format, isSameDay } from "date-fns"; import { format, isSameDay } from "date-fns";
import { Icon } from "@iconify/react"; import { Icon } from "@iconify/react";
import { StickersContext } from "../../../contexts/StickerContext"; import { StickersContext } from "../../../contexts/StickerContext";
@@ -58,7 +58,7 @@ const AddUpdateSticker = ({
const { addEditSticker } = useContext(StickersContext); const { addEditSticker } = useContext(StickersContext);
// ! Update these states to sat "add" and "edit" for easier reading. // ! Update these states to say "add" and "edit" for easier reading.
const [modalVariant] = useState<"currDate" | "notCurrDate">( const [modalVariant] = useState<"currDate" | "notCurrDate">(
isSameDay(date, new Date()) ? "currDate" : "notCurrDate" isSameDay(date, new Date()) ? "currDate" : "notCurrDate"
@@ -75,6 +75,9 @@ const AddUpdateSticker = ({
handleClose(); handleClose();
}; };
// The first sticker to have focus when the modal opens.
const initialRef = useRef();
// * Double check that the submit button is disabled if the selected sticker is the same as the current sticker. // * Double check that the submit button is disabled if the selected sticker is the same as the current sticker.
const variants = { const variants = {
@@ -97,6 +100,7 @@ const AddUpdateSticker = ({
currSticker={currSticker} currSticker={currSticker}
selectedSticker={selectedSticker} selectedSticker={selectedSticker}
updateSelectedSticker={updateSelectedSticker} updateSelectedSticker={updateSelectedSticker}
initialSticker={initialRef}
/> />
</VStack> </VStack>
), ),
@@ -140,6 +144,7 @@ const AddUpdateSticker = ({
currSticker={currSticker} currSticker={currSticker}
selectedSticker={selectedSticker} selectedSticker={selectedSticker}
updateSelectedSticker={updateSelectedSticker} updateSelectedSticker={updateSelectedSticker}
initialSticker={initialRef}
/> />
</VStack> </VStack>
), ),
@@ -223,6 +228,7 @@ const AddUpdateSticker = ({
return ( return (
<Modal <Modal
isCentered isCentered
initialFocusRef={initialRef}
isOpen={isOpen} isOpen={isOpen}
onClose={() => handleClose()} onClose={() => handleClose()}
motionPreset="slideInBottom" motionPreset="slideInBottom"

View File

@@ -7,6 +7,7 @@ interface StickerSelectorProps {
currSticker: StickerVal; currSticker: StickerVal;
selectedSticker: StickerVal; selectedSticker: StickerVal;
updateSelectedSticker: React.Dispatch<React.SetStateAction<StickerVal>>; updateSelectedSticker: React.Dispatch<React.SetStateAction<StickerVal>>;
initialSticker: React.MutableRefObject<undefined>;
} }
/** /**
@@ -21,7 +22,8 @@ const StickerSelector = ({
stickerSet, stickerSet,
currSticker, currSticker,
selectedSticker, selectedSticker,
updateSelectedSticker updateSelectedSticker,
initialSticker
}: StickerSelectorProps): JSX.Element => { }: StickerSelectorProps): JSX.Element => {
const stickers = { const stickers = {
Demo: ( Demo: (
@@ -34,6 +36,7 @@ const StickerSelector = ({
> >
<Button <Button
isDisabled={currSticker >= 1} isDisabled={currSticker >= 1}
ref={currSticker <= 1 ? initialSticker : null}
border={selectedSticker === 1 ? "1px solid #FFF" : "opx"} border={selectedSticker === 1 ? "1px solid #FFF" : "opx"}
bg={selectedSticker === 1 && "gray.800"} bg={selectedSticker === 1 && "gray.800"}
onClick={() => updateSelectedSticker(1)} onClick={() => updateSelectedSticker(1)}
@@ -43,6 +46,7 @@ const StickerSelector = ({
</Button> </Button>
<Button <Button
isDisabled={currSticker === 0} isDisabled={currSticker === 0}
ref={currSticker >= 1 ? initialSticker : null}
border={selectedSticker === 0 ? "1px solid #FFF" : "opx"} border={selectedSticker === 0 ? "1px solid #FFF" : "opx"}
bg={selectedSticker === 0 && "gray.800"} bg={selectedSticker === 0 && "gray.800"}
onClick={() => updateSelectedSticker(0)} onClick={() => updateSelectedSticker(0)}