This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
[Incomplete] Feature: Snackbar implementation #620
Merged
Merged
Changes from all commits
Commits
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,91 @@ | ||
# React Snackbar | ||
|
||
A React version of an [MDC Snackbar](https://github.com/material-components/material-components-web/tree/master/packages/mdc-snackbar). | ||
|
||
## Installation | ||
|
||
``` | ||
npm install @material/react-snackbar | ||
``` | ||
|
||
## Usage | ||
|
||
### Styles | ||
|
||
with Sass: | ||
```js | ||
import '@material/react-snackbar/index.scss'; | ||
``` | ||
|
||
with CSS: | ||
```js | ||
import '@material/react-snackbar/dist/snackbar.css'; | ||
``` | ||
|
||
### Javascript Instantiation | ||
```js | ||
import React from 'react'; | ||
import Snackbar from '@material/react-snackbar'; | ||
|
||
class MyApp extends React.Component { | ||
render() { | ||
return ( | ||
<Snackbar message="Click Me!" actionText="dismiss" /> | ||
); | ||
} | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
Prop Name | Type | Description | ||
gugu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | --- | --- | ||
message | String | Message to show in the snackbar | ||
className | String | Classes to be applied to the root element. | ||
timeoutMs | Number | Timeout in milliseconds when to close snackbar. | ||
closeOnEscape | Boolean | Closes popup on "Esc" button if true. | ||
actionText | String | Text for action button | ||
leading | Boolean | Shows snackbar on the left if true (or right for rtl languages) | ||
stacked | Boolean | Shows buttons under text if true | ||
onAnnounce | Function() => void | Callback for handling screenreader announce event | ||
onOpening | Function() => void | Callback for handling event, which happens before opening | ||
onOpen | Function(evt: Event) => void | Callback for handling event, which happens after opening | ||
onClosing | Function() => void | Callback for handling event, which happens before closing | ||
onClose | Function() => void | Callback for handling event, which happens after closing | ||
|
||
## Getting snackbar parameters | ||
|
||
If you need to get the `timeoutMs`, `closeOnEscape`, or `open` value, then you can use a ref like so: | ||
|
||
```js | ||
import React from 'react'; | ||
gugu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import Snackbar from '@material/react-snackbar'; | ||
class MyApp extends React.Component { | ||
getSnackbarInfo = (snackbar) => { | ||
if (!snackbar) return; | ||
console.log(snackbar.getTimeoutMs()); | ||
console.log(snackbar.isOpen()); | ||
console.log(snackbar.getCloseOnEscape()); | ||
} | ||
render() { | ||
return ( | ||
<Snackbar | ||
message="Click Me!" | ||
actionText="dismiss" | ||
ref={this.getSnackbarInfo} | ||
/> | ||
); | ||
} | ||
} | ||
``` | ||
|
||
## Sass Mixins | ||
|
||
Sass mixins may be available to customize various aspects of the Components. Please refer to the | ||
MDC Web repository for more information on what mixins are available, and how to use them. | ||
|
||
[Advanced Sass Mixins](https://github.com/material-components/material-components-web/blob/master/packages/mdc-snackbar/README.md#sass-mixins) | ||
|
||
## Usage with Icons | ||
|
||
Please see our [Best Practices doc](../../docs/best-practices.md#importing-font-icons) when importing or using icon fonts. |
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,23 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2018 Google, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
@import "@material/snackbar/mdc-snackbar"; |
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,179 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2019 Google, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import * as React from 'react'; | ||
import classnames from 'classnames'; | ||
|
||
// TODO: replace with MDC Web types when available | ||
import {IMDCSnackbarAdapter, IMDCSnackbarFoundation} from './types'; | ||
gugu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// @ts-ignore no .d.ts file | ||
import {MDCSnackbarFoundation} from '@material/snackbar'; | ||
|
||
export interface Props { | ||
message: string; | ||
className?: string; | ||
timeoutMs?: number; | ||
closeOnEscape?: boolean; | ||
actionText?: string; | ||
leading?: boolean; | ||
stacked?: boolean; | ||
open?: boolean; | ||
onOpening?: () => void; | ||
onOpen?: () => void; | ||
onClosing?: (reason: string) => void; | ||
onClose?: (reason: string) => void; | ||
onAnnounce?: () => void; | ||
}; | ||
|
||
type State = { | ||
classes: Set<string>, | ||
}; | ||
|
||
export class Snackbar extends React.Component<Props, State> { | ||
foundation: IMDCSnackbarFoundation | ||
|
||
static defaultProps: Partial<Props> = { | ||
open: true, | ||
stacked: false, | ||
leading: false, | ||
} | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
const {timeoutMs, closeOnEscape, leading, stacked} = this.props; | ||
const classes = new Set(); | ||
if (leading) { | ||
classes.add('mdc-snackbar--leading'); | ||
} | ||
|
||
if (stacked) { | ||
classes.add('mdc-snackbar--stacked'); | ||
} | ||
|
||
this.state = { | ||
classes, | ||
}; | ||
|
||
this.foundation = new MDCSnackbarFoundation(this.adapter); | ||
if (timeoutMs) { | ||
this.foundation.setTimeoutMs(timeoutMs); | ||
} | ||
|
||
if (closeOnEscape) { | ||
this.foundation.setCloseOnEscape(closeOnEscape); | ||
} | ||
} | ||
get adapter(): IMDCSnackbarAdapter { | ||
return { | ||
addClass: (className: string) => { | ||
const {classes} = this.state; | ||
classes.add(className); | ||
this.setState({ | ||
classes, | ||
}); | ||
}, | ||
removeClass: (className: string) => { | ||
const {classes} = this.state; | ||
classes.delete(className); | ||
this.setState({ | ||
classes, | ||
}); | ||
}, | ||
announce: () => { | ||
// Usually it works automatically if this component uses conditional rendering | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this comment mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In MDC Snackbar component there are some hacks to make |
||
this.props.onAnnounce && this.props.onAnnounce(); | ||
}, | ||
notifyOpening: () => { | ||
const {onOpening} = this.props; | ||
if (onOpening) { | ||
onOpening(); | ||
} | ||
}, | ||
notifyOpened: () => { | ||
const {onOpen} = this.props; | ||
if (onOpen) { | ||
onOpen(); | ||
} | ||
}, | ||
notifyClosing: (reason: string) => { | ||
const {onClosing} = this.props; | ||
if (onClosing) { | ||
onClosing(reason); | ||
} | ||
}, | ||
notifyClosed: (reason: string) => { | ||
const {onClose} = this.props; | ||
if (onClose) { | ||
onClose(reason); | ||
} | ||
}, | ||
}; | ||
} | ||
close(action: string) { | ||
this.foundation.close(action); | ||
} | ||
getTimeoutMs() { | ||
return this.foundation.getTimeoutMs(); | ||
} | ||
getCloseOnEscape() { | ||
return this.foundation.getCloseOnEscape(); | ||
} | ||
isOpen() { | ||
return this.foundation.isOpen(); | ||
} | ||
handleKeyDown = (e: React.KeyboardEvent) => { | ||
this.foundation.handleKeyDown(e.nativeEvent); | ||
} | ||
handleActionClick = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
this.foundation.handleActionButtonClick(e.nativeEvent); | ||
} | ||
componentDidMount() { | ||
this.foundation.init(); | ||
if (this.props.open) { | ||
this.foundation.open(); | ||
} | ||
} | ||
componentWillUnmount() { | ||
this.foundation.destroy(); | ||
} | ||
get classes() { | ||
return classnames(this.props.className, 'mdc-snackbar', ...Array.from(this.state.classes)); | ||
} | ||
render() { | ||
return <div className={this.classes} onKeyDown={this.handleKeyDown}> | ||
<div className='mdc-snackbar__surface'> | ||
<div className='mdc-snackbar__label' | ||
role='status' | ||
aria-live='polite'> | ||
{this.props.message} | ||
</div> | ||
{this.props.actionText ? | ||
<div className='mdc-snackbar__actions'> | ||
<button type='button' onClick={this.handleActionClick} className='mdc-button mdc-snackbar__action'> | ||
{this.props.actionText} | ||
</button> | ||
</div> : null} | ||
</div> | ||
</div>; | ||
} | ||
} |
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,26 @@ | ||
{ | ||
"name": "@material/react-snackbar", | ||
"version": "0.0.0", | ||
"description": "Material Components React Snackbar", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"keywords": [ | ||
"mdc web react", | ||
"material components react", | ||
"material design", | ||
"material snackbar", | ||
"materialsnackbar" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/material-components/material-components-web-react.git" | ||
}, | ||
"dependencies": { | ||
"classnames": "^2.2.5", | ||
"react": "^16.4.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,49 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2019 Google, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
// TODO: remove this when MDC Web types are added. | ||
|
||
export interface IMDCSnackbarAdapter { | ||
addClass(className: string): void | ||
removeClass(className: string): void | ||
announce(): void | ||
notifyOpening(): void | ||
notifyOpened(): void | ||
notifyClosing(reason: string): void | ||
notifyClosed(reason: string): void | ||
} | ||
|
||
export interface IMDCSnackbarFoundation { | ||
gugu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
open(): void; | ||
close(action: string): void; | ||
isOpen(): boolean | ||
getTimeoutMs(): number | ||
setTimeoutMs(timeoutMs: number): void | ||
getCloseOnEscape(): boolean | ||
setCloseOnEscape(closeOnEscape: boolean): void | ||
handleKeyDown(event: KeyboardEvent): void | ||
handleActionButtonClick(event: MouseEvent): void | ||
handleActionIconClick(event: MouseEvent): void | ||
init(): void | ||
destroy(): void | ||
adapter_: IMDCSnackbarAdapter | ||
} |
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ const readMaterialPackages = () => { | |
} | ||
} | ||
return dependencies; | ||
} | ||
}; | ||
|
||
|
||
module.exports = {readMaterialPackages}; |
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
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.