Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component: Banner Alert #183

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/components/Alerts/Alerts.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
number,
} from "@storybook/addon-knobs"
import Alert from "./Alert"
import BannerAlert from "./BannerAlert"
import { AlertProps, placementType } from "./types"
import { Button } from ".."
import Icon from "../Icon"
import Alerts from "./"

const Readme = require("./Alerts.README.md")
Expand Down Expand Up @@ -550,6 +552,16 @@ stories
</>
)
})
.add("BannerAlert", () => {
return (
<BannerAlert type="danger">
<Icon type="oWarning" size="18px" fill="white" />
<p style={{ marginLeft: 7 }}>
You don't seem to be connected to Internet.
</p>
</BannerAlert>
)
})

const MyStyledComponent = styled.div`
border: 1px solid red;
Expand Down
24 changes: 24 additions & 0 deletions src/components/Alerts/BannerAlert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react"
import ReactDOM from "react-dom"
import { BannerAlertProps } from "./types"
import { BannerAlertWrapper } from "./StyledAlert"

const BannerAlert = (props: BannerAlertProps) => {
const alertMountNode = document.createElement("div")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a new node in the document, can we instead use the application root node?

This node will otherwise keep lying in the DOM unused, and more will be created everytime we render this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or make it configurable to use a mount node, we can supply mount node form the app

alertMountNode.classList.add("knitui-alerts-root")

document.body.appendChild(alertMountNode)

return (
<div>
{ReactDOM.createPortal(
<BannerAlertWrapper customProps={{ type: props.type || "warning" }}>
{props.children}
</BannerAlertWrapper>,
alertMountNode
)}
</div>
)
}

export default BannerAlert
15 changes: 15 additions & 0 deletions src/components/Alerts/StyledAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,18 @@ export const AlertActionsWrapper = styled.span<IStyledAlert>`
margin-right: 0.7rem;
}
`

export const BannerAlertWrapper = styled.div<IStyledAlert>`
position: fixed;
top: 0;
left: 0;
right: 0;
height: 4.8rem;
padding: 0 2.8rem;
display: flex;
align-items: center;
color: white;
font-size: 1.4rem;
line-height: 2rem;
background-color: ${props => getBackgroundColor(props)};
`
9 changes: 6 additions & 3 deletions src/components/Alerts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { FC, Component, ComponentClass } from "react"
import { FC, ComponentClass } from "react"
import Alert from "./Alert"
import BannerAlert from "./BannerAlert"
import AlertsProvider from "./AlertsProvider"
import useAlerts from "./useAlerts"
import withAlerts from "./withAlerts"

import { AlertProps, useAlertType } from "./types"
import { AlertProps, useAlertType, BannerAlertProps } from "./types"

interface IAlertWrapper {
Alert: FC<AlertProps>
BannerAlert: FC<BannerAlertProps>
AlertsProvider: ComponentClass
useAlerts: useAlertType
withAlerts: (component: any) => any
}

const AlertWrapper: IAlertWrapper = {
Alert,
BannerAlert,
AlertsProvider,
useAlerts,
withAlerts
withAlerts,
}

export default AlertWrapper
7 changes: 6 additions & 1 deletion src/components/Alerts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ export type AlertProviderType = Component<{}, AlertsProviderState>
export type useAlertType = () => {
addAlert: addAlertType
removeAlert: removeAlertType
}
}

export interface BannerAlertProps {
children: ReactNode
type: string
}