This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for a loading indicator
Summary: After running in a ZND, it became obvious that a loading indicator was necessary for this component so I modeled it (visually) after the `react-select` loading indicator. GIF demo: {F669626} Test Plan: npm run lint npm run flow Reviewers: riley, alex Reviewed By: alex Differential Revision: https://phabricator.khanacademy.org/D34646
- Loading branch information
1 parent
b0e809a
commit bc226be
Showing
6 changed files
with
123 additions
and
14 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
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,78 @@ | ||
// @flow | ||
/** | ||
* A simple loading indicator, modeled after react-select. Since react styles | ||
* don't support animations, hack it so we inject the keyframe animation | ||
* into the document. | ||
*/ | ||
import React, {Component} from 'react'; | ||
|
||
const STYLESHEET_NAME = "__react-multi-select_style_inject__"; | ||
|
||
|
||
function findStylesheet(): ?CSSStyleSheet { | ||
const styleSheet = Array.from(document.styleSheets) | ||
.find(stylesheet => stylesheet.title === STYLESHEET_NAME); | ||
|
||
// upcast as CSSStyleSheet | ||
const cssStylesheet: ?CSSStyleSheet = (styleSheet: any); | ||
|
||
return cssStylesheet; | ||
} | ||
|
||
function registerStylesheet(css) { | ||
try { | ||
if (findStylesheet()) { | ||
return; | ||
} | ||
|
||
const style = document.createElement("style"); | ||
style.setAttribute("title", STYLESHEET_NAME); | ||
document.head && document.head.appendChild(style); | ||
|
||
const stylesheet = findStylesheet(); | ||
if (!stylesheet) { | ||
// Someting bad happened. Abort! | ||
return; | ||
} | ||
|
||
stylesheet.insertRule(css, 0); | ||
} catch (e) { | ||
} | ||
} | ||
|
||
class LoadingIndicator extends Component { | ||
componentWillMount() { | ||
// React styles don't support adding keyframe rules. Create a | ||
// stylesheet and inject the keyframe animarion into it. | ||
registerStylesheet(keyFrames); | ||
} | ||
|
||
render() { | ||
return <span style={styles.loading} />; | ||
} | ||
} | ||
|
||
const keyFrames = ` | ||
@keyframes react-multi-select_loading-spin { | ||
to { | ||
transform: rotate(1turn); | ||
} | ||
} | ||
`; | ||
|
||
const styles = { | ||
loading: { | ||
"animation": "react-multi-select_loading-spin 400ms infinite linear", | ||
"width": "16px", | ||
"height": "16px", | ||
boxSizing: "border-box", | ||
borderRadius: "50%", | ||
border: "2px solid #ccc", | ||
borderRightColor: "#333", | ||
display: "inline-block", | ||
position: "relative", | ||
verticalAlign: "middle", | ||
}, | ||
}; | ||
|
||
export default LoadingIndicator; |
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,6 @@ | ||
import React from 'react'; | ||
import {storiesOf} from '@kadira/storybook'; | ||
import LoadingIndicator from '../loading-indicator.js'; | ||
|
||
storiesOf('Loading Indicator', module) | ||
.add('default view', () => <LoadingIndicator />); |