This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
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.
feat(popper): embedded react-popper library
- Loading branch information
1 parent
8fab4bf
commit 0b43f4c
Showing
7 changed files
with
141 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Popper } from './src/Popper'; |
47 changes: 47 additions & 0 deletions
47
packages/components/src/components/Popper/src/Popper.stories.tsx
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,47 @@ | ||
import React, { useState } from 'react'; | ||
import { withKnobs, text, select, boolean } from '@storybook/addon-knobs'; | ||
import { Popper, PopperPlacement } from './Popper'; | ||
import { Button } from '../../Button'; | ||
|
||
export default { | ||
title: 'Popper', | ||
decorators: [withKnobs], | ||
}; | ||
|
||
const placements: PopperPlacement[] = [ | ||
'auto', | ||
'auto-start', | ||
'auto-end', | ||
'top', | ||
'bottom', | ||
'right', | ||
'left', | ||
'top-start', | ||
'top-end', | ||
'bottom-start', | ||
'bottom-end', | ||
'right-start', | ||
'right-end', | ||
'left-start', | ||
'left-end', | ||
]; | ||
|
||
export const Default = () => { | ||
const [isShown, setShown] = useState(false); | ||
return ( | ||
<div | ||
style={{ | ||
padding: 20, | ||
}} | ||
> | ||
<Popper | ||
trigger={ | ||
<Button onClick={() => setShown((prev) => !prev)}>trigger</Button> | ||
} | ||
placement={select('placement', placements, 'auto')} | ||
> | ||
{isShown ? <div>popperElement</div> : undefined} | ||
</Popper> | ||
</div> | ||
); | ||
}; |
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,73 @@ | ||
import React, { useState, useRef, useMemo } from 'react'; | ||
import { Placement, Options } from '@popperjs/core'; | ||
import { usePopper } from 'react-popper-2'; | ||
import { Portal } from '../../Portal'; | ||
|
||
export type PopperPlacement = Placement; | ||
export interface PopperProps { | ||
trigger: React.ReactElement; | ||
children?: React.ReactElement; | ||
/** Formatted like "0, 8px" — how far to offset the Popper from the Reference. Changes automatically based on the placement */ | ||
offset?: [number | null | undefined, number | null | undefined]; | ||
placement?: Placement; | ||
} | ||
|
||
export const Popper: React.FC<PopperProps> = ({ | ||
trigger, | ||
children, | ||
placement = 'bottom', | ||
offset = [0, 8], | ||
}) => { | ||
const referenceElement = useRef<any>(); | ||
const popperElement = useRef<any>(); | ||
|
||
const options: Options = useMemo(() => { | ||
return { | ||
placement: placement, | ||
strategy: 'fixed', | ||
modifiers: [ | ||
{ | ||
name: 'hide', | ||
}, | ||
{ | ||
name: 'offset', | ||
options: { | ||
offset: offset, | ||
}, | ||
}, | ||
{ | ||
name: 'preventOverflow', | ||
options: { | ||
altAxis: true, | ||
padding: 40, | ||
}, | ||
}, | ||
], | ||
}; | ||
}, [placement, offset]); | ||
|
||
const { styles, attributes } = usePopper( | ||
referenceElement.current, | ||
popperElement.current, | ||
options, | ||
); | ||
|
||
return ( | ||
<React.Fragment> | ||
{/** trigger element */} | ||
{React.cloneElement(trigger, { | ||
...trigger.props, | ||
ref: referenceElement, | ||
})} | ||
<Portal> | ||
{children && | ||
React.cloneElement(children, { | ||
style: styles.popper, | ||
...attributes.popper, | ||
...children.props, | ||
ref: popperElement, | ||
})} | ||
</Portal> | ||
</React.Fragment> | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -20,4 +20,5 @@ export { | |
Typography, | ||
TypographyContext, | ||
Paragraph, | ||
Popper, | ||
} from './components'; |
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