This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bounty feat: Add NotificationDot component (#208)
* feat: Add NotificationDot component * refactor: Use positive show prop instead of negative invisible prop
- Loading branch information
Showing
6 changed files
with
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react"; | ||
import { renderWithTheme } from "../../testHelpers"; | ||
import NotificationDot from "../../components/NotificationDot/NotificationDot"; | ||
|
||
it("renders correctly", () => { | ||
const { asFragment } = renderWithTheme( | ||
<NotificationDot> | ||
<div /> | ||
</NotificationDot> | ||
); | ||
expect(asFragment()).toMatchInlineSnapshot(` | ||
<DocumentFragment> | ||
<span | ||
class="sc-bdfBwQ gfxPDG" | ||
> | ||
<div /> | ||
<span | ||
class="sc-gsTCUz cfVzjW" | ||
/> | ||
</span> | ||
</DocumentFragment> | ||
`); | ||
}); |
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,30 @@ | ||
import React, { cloneElement, Children, ReactElement } from "react"; | ||
import styled from "styled-components"; | ||
import { NotificationDotProps, DotProps } from "./types"; | ||
|
||
const NotificationDotRoot = styled.span` | ||
display: inline-flex; | ||
position: relative; | ||
`; | ||
|
||
const Dot = styled.span<DotProps>` | ||
display: ${({ show }) => (show ? "inline-flex" : "none")}; | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
width: 10px; | ||
height: 10px; | ||
pointer-events: none; | ||
border: 1px solid ${({ theme }) => theme.colors.invertedContrast}; | ||
border-radius: 50%; | ||
background-color: ${({ theme }) => theme.colors.failure}; | ||
`; | ||
|
||
const NotificationDot: React.FC<NotificationDotProps> = ({ show = false, children, ...props }) => ( | ||
<NotificationDotRoot> | ||
{Children.map(children, (child: ReactElement) => cloneElement(child, props))} | ||
<Dot show={show} /> | ||
</NotificationDotRoot> | ||
); | ||
|
||
export default NotificationDot; |
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,49 @@ | ||
import React, { useState } from "react"; | ||
import styled from "styled-components"; | ||
import NotificationDot from "./NotificationDot"; | ||
import Button from "../Button/Button"; | ||
import ButtonMenu from "../ButtonMenu/ButtonMenu"; | ||
import ButtonMenuItem from "../ButtonMenu/ButtonMenuItem"; | ||
|
||
export default { | ||
title: "Components/NotificationDot", | ||
component: NotificationDot, | ||
argTypes: {}, | ||
}; | ||
|
||
export const Default: React.FC = () => { | ||
return ( | ||
<NotificationDot show> | ||
<Button>Hi</Button> | ||
</NotificationDot> | ||
); | ||
}; | ||
|
||
const Row = styled.div` | ||
& > * + * { | ||
margin-left: 16px; | ||
} | ||
`; | ||
|
||
export const MenuButtons: React.FC = () => { | ||
const [index, setIndex] = useState(0); | ||
const handleClick = (newIndex) => setIndex(newIndex); | ||
return ( | ||
<Row> | ||
<ButtonMenu activeIndex={index} onItemClick={handleClick}> | ||
<NotificationDot show={index === 0}> | ||
<ButtonMenuItem>Button 1</ButtonMenuItem> | ||
</NotificationDot> | ||
<NotificationDot show={index === 1}> | ||
<ButtonMenuItem>Button 2</ButtonMenuItem> | ||
</NotificationDot> | ||
<NotificationDot show={index === 2}> | ||
<ButtonMenuItem>Button 3</ButtonMenuItem> | ||
</NotificationDot> | ||
<NotificationDot show={index === 3}> | ||
<ButtonMenuItem>Button 4</ButtonMenuItem> | ||
</NotificationDot> | ||
</ButtonMenu> | ||
</Row> | ||
); | ||
}; |
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,2 @@ | ||
export { default as NotificationDot } from "./NotificationDot"; | ||
export type { NotificationDotProps, DotProps } from "./types"; |
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,8 @@ | ||
export interface NotificationDotProps { | ||
show?: boolean; | ||
children: React.ReactElement | React.ReactElement[]; | ||
} | ||
|
||
export interface DotProps { | ||
show: boolean; | ||
} |
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