-
Notifications
You must be signed in to change notification settings - Fork 0
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 #483 from atls/feat/utils
feat: added useHover and usePopover in utils
- Loading branch information
Showing
8 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@atls-utils/use-hover", | ||
"version": "0.0.0", | ||
"license": "BSD-3-Clause", | ||
"main": "src/index.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn library build", | ||
"prepack": "yarn run build", | ||
"postpack": "rm -rf dist" | ||
}, | ||
"dependencies": { | ||
"react-laag": "2.0.5" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts" | ||
} | ||
} |
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 { useHover } from 'react-laag' |
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,26 @@ | ||
{ | ||
"name": "@atls-utils/use-popover", | ||
"version": "0.0.0", | ||
"license": "BSD-3-Clause", | ||
"main": "src/index.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn library build", | ||
"prepack": "yarn run build", | ||
"postpack": "rm -rf dist" | ||
}, | ||
"dependencies": { | ||
"react-laag": "2.0.5" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.2.20", | ||
"react": "18.2.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts" | ||
} | ||
} |
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 { usePopover } from './use-popover.hook' |
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,48 @@ | ||
import { PlacementType } from 'react-laag/dist/PlacementType' | ||
import { useState } from 'react' | ||
import { useLayer } from 'react-laag' | ||
|
||
export const usePopover = (placement: PlacementType, offset = 9, trigger = 'click') => { | ||
const [isOpen, setIsOpen] = useState<boolean>(false) | ||
|
||
const close = () => setIsOpen(false) | ||
|
||
const { layerProps, triggerProps, renderLayer } = useLayer({ | ||
isOpen, | ||
placement, | ||
auto: true, | ||
onOutsideClick: close, | ||
onDisappear: close, | ||
triggerOffset: offset, | ||
}) | ||
|
||
const setTrigger = (value) => { | ||
if (value === 'click') { | ||
return { ...triggerProps, onClick: () => setIsOpen(!isOpen) } | ||
} | ||
if (value === 'hover') { | ||
return { | ||
...triggerProps, | ||
onMouseEnter: () => setIsOpen(true), | ||
onMouseLeave: () => false, | ||
} | ||
} | ||
|
||
return {} | ||
} | ||
|
||
const render = (children) => renderLayer(isOpen && children) | ||
|
||
return { | ||
triggerProps: setTrigger(trigger), | ||
layerProps: { | ||
...layerProps, | ||
style: { | ||
...layerProps.style, | ||
}, | ||
}, | ||
render, | ||
isOpen, | ||
setIsOpen, | ||
} | ||
} |
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