-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tap area to checkbox radio and toggle #242
- Loading branch information
Showing
7 changed files
with
93 additions
and
13 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
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,15 +1,22 @@ | ||
import { FC } from "react"; | ||
import { FC, useRef } from "react"; | ||
import { Radio as BaseRadio, RadioProps as BaseRadioProps } from "baseui/radio"; | ||
import { getRadioOverrides } from "./overrides"; | ||
import { getMergedOverrides } from "../../shared/utils/getMergedOverrides"; | ||
import { TapArea } from "../../shared/ui/tap-area"; | ||
|
||
export type RadioProps = BaseRadioProps; | ||
|
||
const Radio: FC<RadioProps> = ({ overrides: baseOverrides, ...props }) => { | ||
const Radio: FC<RadioProps> = ({ overrides: baseOverrides, inputRef, ...props }) => { | ||
const radioOverrides = getRadioOverrides(); | ||
const overrides = getMergedOverrides(radioOverrides, baseOverrides); | ||
const radioRef = useRef<HTMLInputElement>(null); | ||
const finalRef = inputRef || radioRef; | ||
|
||
return <BaseRadio overrides={overrides} {...props} />; | ||
return ( | ||
<TapArea onClick={() => finalRef.current?.click()}> | ||
<BaseRadio overrides={overrides} inputRef={finalRef} {...props} /> | ||
</TapArea> | ||
); | ||
}; | ||
|
||
export default Radio; |
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,43 @@ | ||
import { MouseEvent, ReactNode } from "react"; | ||
import { StyleObject, useStyletron } from "styletron-react"; | ||
import { isTouch } from "../../utils/isTouch"; | ||
|
||
type TapAreaProps = { | ||
tapAreaSize?: number; | ||
children: ReactNode; | ||
onClick: () => void; | ||
}; | ||
|
||
const getOuterStyle = (tapAreaSize: TapAreaProps["tapAreaSize"]): StyleObject => ({ | ||
minWidth: `${tapAreaSize}px`, | ||
height: `${tapAreaSize}px`, | ||
display: "inline-flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
userSelect: "none", | ||
}); | ||
|
||
const isTouchDevice = isTouch(); // we can optimistically check this once because it won't change in 99.9% cases | ||
|
||
const TapArea = ({ tapAreaSize = 48, children, onClick }: TapAreaProps) => { | ||
const [css] = useStyletron(); | ||
|
||
if (!isTouchDevice) { | ||
return <>{children}</>; | ||
} | ||
|
||
const outerStyle = css(getOuterStyle(tapAreaSize)); | ||
const onClickWithStopPropagation = (event: MouseEvent<HTMLDivElement>) => { | ||
onClick(); | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
}; | ||
|
||
return ( | ||
<div onClick={onClickWithStopPropagation} className={outerStyle}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default TapArea; |
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 @@ | ||
export { default as TapArea } from "./TapArea"; |
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,7 @@ | ||
export const isTouch = () => { | ||
if (typeof window === "undefined") { | ||
return false; | ||
} | ||
|
||
return "ontouchstart" in window || navigator.maxTouchPoints > 0 || navigator.maxTouchPoints > 0; | ||
}; |