mirror of
https://github.com/vercel/commerce.git
synced 2025-07-29 05:01:22 +00:00
.github
.vscode
packages
site
assets
components
config
lib
api
click-outside
click-outside.tsx
has-parent.js
index.ts
is-in-dom.js
hooks
colors.ts
focus-trap.tsx
get-slug.ts
range-map.ts
search-props.tsx
search.tsx
to-pixels.ts
usage-warns.ts
pages
public
.env.template
.eslintrc
.gitignore
.npmrc
.prettierignore
.prettierrc
commerce-config.js
commerce.config.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
.editorconfig
.gitignore
.prettierignore
.prettierrc
README.md
license.md
package.json
pnpm-lock.yaml
pnpm-workspace.yaml
turbo.json
* Updated log * Updates to root * Updates to pnpm * successfully moved to pnpm * type issue * Local as the default provider * Upgrade dependencies * Revert to local * Upgrade React * Update node-fetch deps * Fix types * Ignore warnings * Fix missing dependency * Update pnpm-lock.yaml * Add missing @types/cookie * Upgrade dependencies * Fix missing dependencies * Update README.md Co-authored-by: Bel Curcio <curciobel@gmail.com>
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
import React, {
|
|
useRef,
|
|
useEffect,
|
|
MouseEvent,
|
|
FC,
|
|
ReactElement,
|
|
forwardRef,
|
|
Ref,
|
|
ReactNode,
|
|
} from 'react'
|
|
import { mergeRefs } from 'react-merge-refs'
|
|
import hasParent from './has-parent'
|
|
|
|
interface ClickOutsideProps {
|
|
active: boolean
|
|
onClick: (e?: MouseEvent) => void
|
|
ref?: Ref<any>
|
|
children?: ReactNode
|
|
}
|
|
|
|
/**
|
|
* Use forward ref to allow this component to be used with other components like
|
|
* focus-trap-react, that rely on the same type of ref forwarding to direct children
|
|
*/
|
|
const ClickOutside: FC<ClickOutsideProps> = forwardRef(
|
|
({ active = true, onClick, children }, forwardedRef) => {
|
|
const innerRef = useRef()
|
|
|
|
const child = children ? (React.Children.only(children) as any) : undefined
|
|
|
|
if (!child || child.type === React.Fragment) {
|
|
/**
|
|
* React Fragments can't be used, as it would not be possible to pass the ref
|
|
* created here to them.
|
|
*/
|
|
throw new Error('A valid non Fragment React Children should be provided')
|
|
}
|
|
|
|
if (typeof onClick != 'function') {
|
|
throw new Error('onClick must be a valid function')
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (active) {
|
|
document.addEventListener('mousedown', handleClick)
|
|
document.addEventListener('touchstart', handleClick)
|
|
}
|
|
return () => {
|
|
if (active) {
|
|
document.removeEventListener('mousedown', handleClick)
|
|
document.removeEventListener('touchstart', handleClick)
|
|
}
|
|
}
|
|
})
|
|
|
|
const handleClick = (event: any) => {
|
|
/**
|
|
* Check if the clicked element is contained by the top level tag provided to the
|
|
* ClickOutside component, if not, Outside clicked! Fire onClick cb
|
|
*/
|
|
if (!hasParent(event.target, innerRef?.current)) {
|
|
onClick(event)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Preserve the child ref prop if exists and merge it with the one used here and the
|
|
* proxied by the forwardRef method
|
|
*/
|
|
const composedRefCallback = (element: ReactElement) => {
|
|
if (typeof child.ref === 'function') {
|
|
child.ref(element)
|
|
} else if (child.ref) {
|
|
child.ref.current = element
|
|
}
|
|
}
|
|
|
|
return React.cloneElement(child, {
|
|
ref: mergeRefs([composedRefCallback, innerRef, forwardedRef]),
|
|
})
|
|
}
|
|
)
|
|
|
|
ClickOutside.displayName = 'ClickOutside'
|
|
export default ClickOutside
|