This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Add an Alert component #349
Merged
johannadevos
merged 14 commits into
develop
from
13173-add-notification-no-organization
Sep 10, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e9c3a07
Add 4 type of alert icons to the SvgIcon component
igorschoester 452d286
Add Alert component
igorschoester 4502056
Add tests for the Alert component
igorschoester ffa3bbd
Export Alert in the index
igorschoester 0206010
Add Alerts in the component example
igorschoester 2cd1c32
Fix CS
igorschoester a60938e
Remove double snapshots and update outdated snapshots
igorschoester 6e53419
Add status colors to the palette
igorschoester c860c8e
Adapt the Alert to design specs
igorschoester 48a9898
Rename colors props.
afercia 5796c9e
Improve icon vertical alignment.
afercia 1f3ee7b
Avoid to pass translatable string via default prop.
afercia 5147137
Dismiss button bigger clickable area and focus style.
afercia d2af54a
Add new focus style based on new colors.
afercia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,161 @@ | ||
/* External dependencies */ | ||
import PropTypes from "prop-types"; | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
import { __ } from "@wordpress/i18n"; | ||
|
||
/* Yoast dependencies */ | ||
import { getDirectionalStyle } from "@yoast/helpers"; | ||
import { colors } from "@yoast/style-guide"; | ||
|
||
/* Internal dependencies */ | ||
import Button from "./buttons/Button"; | ||
import SvgIcon from "./SvgIcon"; | ||
|
||
const AlertContainer = styled.div` | ||
display: flex; | ||
align-items: flex-start; | ||
font-size: 14px; | ||
line-height: 1.5; | ||
border: 1px solid rgba(0, 0, 0, 0.2); | ||
padding: 16px; | ||
color: ${ props => props.alertColor }; | ||
background: ${ props => props.alertBackground }; | ||
margin-bottom: 20px; | ||
`; | ||
|
||
const AlertContent = styled.div` | ||
flex-grow: 1; | ||
|
||
a { | ||
color: ${ colors.$color_alert_link_text }; | ||
} | ||
`; | ||
|
||
const AlertIcon = styled( SvgIcon )` | ||
margin-top: 0.125rem; | ||
${ getDirectionalStyle( "margin-right: 8px", "margin-left: 8px" ) }; | ||
`; | ||
|
||
const AlertDismiss = styled( Button )` | ||
${ getDirectionalStyle( "margin: -8px -12px -8px 8px", "margin: -8px 8px -12px -8px" ) }; | ||
font-size: 24px; | ||
line-height: 1.4; | ||
color: ${ props => props.alertDismissColor }; | ||
flex-shrink: 0; | ||
min-width: 36px; | ||
height: 36px; | ||
|
||
// Override the base button style: get rid of the button styling. | ||
padding: 0; | ||
|
||
&, &:hover, &:active { | ||
/* Inherits box-sizing: border-box so this doesn't change the rendered size. */ | ||
border: 2px solid transparent; | ||
background: transparent; | ||
box-shadow: none; | ||
color: ${ props => props.alertDismissColor }; | ||
} | ||
|
||
/* Inherits focus style from the Button component. */ | ||
&:focus { | ||
background: transparent; | ||
color: ${ props => props.alertDismissColor }; | ||
border-color: ${ colors.$color_yoast_focus }; | ||
box-shadow: 0px 0px 0px 3px ${ colors.$color_yoast_focus_outer }; | ||
} | ||
`; | ||
|
||
/** | ||
* Alert component. | ||
* | ||
* @param {Object} props The props to use. | ||
* @param {*} props.children The children to render inside the alert. | ||
* @param {string} props.type The type of Alert. Can be: "error", "info", "success" or "warning". | ||
* Controls the colors and icon of the Alert. | ||
* @param {Function} [props.onDismissed] When supplied this Alert will gain an 'X' button. | ||
* Note: the function provided should do the actual dismissing! | ||
* @param {string} dismissAriaLabel The close button aria-label. Must be a translatable string with | ||
* text domain. | ||
* | ||
* @returns {Alert} The Alert component. | ||
*/ | ||
class Alert extends React.Component { | ||
/** | ||
* Returns the colors and icon to be used based on the type provided to the props. | ||
* | ||
* @param {string} type The type of Alert. | ||
* | ||
* @returns {object} Options with colors and icons to be used. | ||
*/ | ||
getTypeDisplayOptions( type ) { | ||
switch ( type ) { | ||
case "error": | ||
return { | ||
color: colors.$color_alert_error_text, | ||
background: colors.$color_alert_error_background, | ||
icon: "alert-error", | ||
}; | ||
case "info": | ||
return { | ||
color: colors.$color_alert_info_text, | ||
background: colors.$color_alert_info_background, | ||
icon: "alert-info", | ||
}; | ||
case "success": | ||
return { | ||
color: colors.$color_alert_success_text, | ||
background: colors.$color_alert_success_background, | ||
icon: "alert-success", | ||
}; | ||
case "warning": | ||
return { | ||
color: colors.$color_alert_warning_text, | ||
background: colors.$color_alert_warning_background, | ||
icon: "alert-warning", | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Renders the component. | ||
* | ||
* @returns {React.Element} The rendered component. | ||
*/ | ||
render() { | ||
const options = this.getTypeDisplayOptions( this.props.type ); | ||
const dismissAriaLabel = this.props.dismissAriaLabel || __( "Dismiss this alert", "yoast-components" ); | ||
|
||
return <AlertContainer alertColor={ options.color } alertBackground={ options.background }> | ||
<AlertIcon icon={ options.icon } color={ options.color } /> | ||
<AlertContent>{ this.props.children }</AlertContent> | ||
{ | ||
typeof this.props.onDismissed === "function" | ||
? ( | ||
<AlertDismiss | ||
alertDismissColor={ options.color } | ||
onClick={ this.props.onDismissed } | ||
aria-label={ dismissAriaLabel } | ||
> | ||
× | ||
</AlertDismiss> | ||
) | ||
: null | ||
} | ||
</AlertContainer>; | ||
} | ||
} | ||
|
||
Alert.propTypes = { | ||
children: PropTypes.any.isRequired, | ||
type: PropTypes.oneOf( [ "error", "info", "success", "warning" ] ).isRequired, | ||
onDismissed: PropTypes.func, | ||
dismissAriaLabel: PropTypes.string, | ||
}; | ||
|
||
Alert.defaultProps = { | ||
onDismissed: null, | ||
dismissAriaLabel: "", | ||
}; | ||
|
||
export default Alert; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,95 @@ | ||
import React from "react"; | ||
import renderer from "react-test-renderer"; | ||
|
||
import Alert from "../src/Alert"; | ||
|
||
describe( "Alert", () => { | ||
test( "the Alert types match the snapshot", () => { | ||
const alerts = [ | ||
{ | ||
type: "error", | ||
color: "#8f1919", | ||
background: "#f9dcdc", | ||
icon: "alert-error", | ||
}, | ||
{ | ||
type: "info", | ||
color: "#00468f", | ||
background: "#cce5ff", | ||
icon: "alert-info", | ||
}, | ||
{ | ||
type: "success", | ||
color: "#395315", | ||
background: "#e2f2cc", | ||
icon: "alert-success", | ||
}, | ||
{ | ||
type: "warning", | ||
color: "#674e00", | ||
background: "#fff3cd", | ||
icon: "alert-warning", | ||
}, | ||
]; | ||
|
||
alerts.forEach( alert => { | ||
const content = `This is of the type: "${ alert.type }".`; | ||
const component = renderer.create( | ||
<Alert type={ alert.type }> | ||
{ content } | ||
</Alert>, | ||
); | ||
const tree = component.toJSON(); | ||
|
||
// Check the color of the alert. | ||
expect( tree.props.color ).toBe( alert.alertColor ); | ||
|
||
// 2 children: The type icon and the content. | ||
expect( tree.children.length ).toBe( 2 ); | ||
|
||
// Check the icon. | ||
expect( tree.children[ 0 ].props.className.indexOf( alert.icon ) ).not.toBe( -1 ); | ||
expect( tree.children[ 0 ].props.fill ).toBe( alert.color ); | ||
|
||
// Check the content. | ||
expect( tree.children[ 1 ].type ).toBe( "div" ); | ||
expect( tree.children[ 1 ].children.length ).toBe( 1 ); | ||
expect( tree.children[ 1 ].children[ 0 ] ).toBe( content ); | ||
|
||
// Match the snapshot. | ||
expect( tree ).toMatchSnapshot(); | ||
|
||
// Check the background of the alert. | ||
const { rendered } = component.toTree(); | ||
expect( rendered.props.background ).toBe( alert.alertBackground ); | ||
} ); | ||
} ); | ||
|
||
test( "passing onDismissed makes the alert dismissable", () => { | ||
const dismiss = jest.fn(); | ||
const component = renderer.create( | ||
<Alert type="info" onDismissed={ dismiss }> | ||
Dismissable alert. | ||
</Alert>, | ||
); | ||
const tree = component.toJSON(); | ||
|
||
// 3 children: The type icon, the content and the dismiss button. | ||
expect( tree.children.length ).toBe( 3 ); | ||
|
||
// Check the last child is the dismissable button. | ||
expect( tree.children[ 2 ].type ).toBe( "button" ); | ||
|
||
// Inside the button should be the times symbol. | ||
expect( tree.children[ 2 ].children.length ).toBe( 1 ); | ||
expect( tree.children[ 2 ].children[ 0 ] ).toBe( "×" ); | ||
|
||
// Check the dismiss action. | ||
expect( typeof tree.children[ 2 ].props.onClick ).toBe( "function" ); | ||
tree.children[ 2 ].props.onClick(); | ||
expect( dismiss ).toHaveBeenCalledTimes( 1 ); | ||
|
||
// Match the snapshot. | ||
expect( tree ).toMatchSnapshot(); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.