This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react-tiny-popover and labs TourPoint component
- Loading branch information
1 parent
be00f36
commit edd2cb5
Showing
4 changed files
with
154 additions
and
0 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,71 @@ | ||
import React, { forwardRef } from 'react'; | ||
|
||
import { Story } from '@storybook/react'; | ||
|
||
import { TourPoint } from './TourPoint'; | ||
|
||
import { Header } from '../../typography'; | ||
|
||
export default { | ||
title: 'labs/TourPoint', | ||
component: TourPoint, | ||
argTypes: { | ||
icon: { control: { disable: true } }, | ||
onClose: { control: { disable: true } }, | ||
target: { control: { disable: true } }, | ||
}, | ||
excludeStories: ['Card'], | ||
}; | ||
|
||
type Props = any; | ||
|
||
new Image().src = 'http://placekitten.com/320/213'; | ||
|
||
const Template: Story<Props> = (args) => { | ||
return ( | ||
<div | ||
style={{ | ||
padding: '7rem', | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: '2rem', | ||
height: '500px', | ||
}} | ||
> | ||
<TourPoint | ||
active={true} | ||
attach="left" | ||
{...args} | ||
src="http://placekitten.com/320/213" | ||
step={1} | ||
title="A Fresh New Look" | ||
content="All the leaves are brown and the sky is grey, I've been for a walk on a winters day." | ||
onClick={() => console.log('tourpoint clicked')} | ||
> | ||
<Card>1</Card> | ||
</TourPoint> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Controls = Template.bind({}); | ||
Controls.storyName = 'TourPoint'; | ||
Controls.args = {}; | ||
|
||
export const Card: any = forwardRef(({ children }, ref: any) => { | ||
return ( | ||
<div | ||
ref={ref} | ||
style={{ | ||
width: '12rem', | ||
height: '12rem', | ||
background: 'rgba(150,150,150,0.5)', | ||
padding: '1rem', | ||
borderRadius: '0.25rem', | ||
margin: '4rem auto', | ||
}} | ||
> | ||
<Header size="3">{children}</Header> | ||
</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,76 @@ | ||
import React from 'react'; | ||
import { | ||
Popover, | ||
PopoverPosition, | ||
PopoverAlign, | ||
} from 'react-tiny-popover'; | ||
import { TourPointStyled } from '../../components/TourPoint/TourPoint.style'; | ||
import { Props as TourPointProps } from '../../components/TourPoint/TourPoint.types'; | ||
import { AttachAlias } from '../../utils'; | ||
|
||
interface Props extends TourPointProps { | ||
children: JSX.Element; | ||
positions?: PopoverPosition[]; | ||
align: PopoverAlign; | ||
} | ||
|
||
type Attach = | ||
| 'right' | ||
| 'right-top' | ||
| 'right-bottom' | ||
| 'left' | ||
| 'left-top' | ||
| 'left-bottom' | ||
| 'top' | ||
| 'top-left' | ||
| 'top-right' | ||
| 'bottom' | ||
| 'bottom-left' | ||
| 'bottom-right'; | ||
|
||
export function TourPoint({ | ||
active, | ||
content, | ||
children, | ||
// legacy | ||
attach, | ||
positions, | ||
align, | ||
}: Props) { | ||
let pos = positions; | ||
let alignment = align; | ||
if (attach && !positions) { | ||
[pos, alignment] = convertAttachToPositionAlign(attach); | ||
} | ||
|
||
return ( | ||
<Popover | ||
isOpen={!!active} | ||
positions={pos} | ||
align={alignment} | ||
content={<TourPointStyled>{content}</TourPointStyled>} | ||
> | ||
{children} | ||
</Popover> | ||
); | ||
} | ||
|
||
function convertAttachToPositionAlign( | ||
attach: Attach | ||
): [PopoverPosition[], PopoverAlign] { | ||
const [side, placement] = attach.split('-'); | ||
let align: PopoverAlign = 'center'; | ||
switch (placement) { | ||
case 'top': | ||
case 'left': | ||
align = 'start'; | ||
break; | ||
case 'bottom': | ||
case 'right': | ||
align = 'end'; | ||
default: | ||
break; | ||
} | ||
|
||
return [[side as PopoverPosition], align]; | ||
} |
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