-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GifPlayer): adds component to pause and play gifs for a11y (#676)
* v1.17.2 * v1.17.3 * feat: add gif player to pause and play gifs * feat: test gif player in image gallery * fix: gif player in dialog and hidden styles * fix: gif player inside image gallery * fix: gif player example images * fix: selector names * Update packages/gatsby-theme-carbon/src/components/GifPlayer/GifPlayer.js Co-Authored-By: DAK <40970507+dakahn@users.noreply.github.com> * Update packages/gatsby-theme-carbon/src/components/GifPlayer/GifPlayer.js Co-Authored-By: DAK <40970507+dakahn@users.noreply.github.com> * fix: focus for ff * fix: pause play label Co-authored-by: DAK <40970507+dakahn@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
273 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
title: GifPlayer | ||
description: Usage instructions for the Accordion component | ||
--- | ||
|
||
<PageDescription> | ||
|
||
The `<GifPlayer>` component is used to pause and play images that are gif's. It works by replacing the gif with a static image on pause. | ||
|
||
</PageDescription> | ||
|
||
## Example | ||
|
||
<Row> | ||
<Column colLg='4'> | ||
|
||
<Title>Light</Title> | ||
|
||
<GifPlayer> | ||
|
||
![IBM Cloud Pictograms](/images/IBM_Cloud_Pictograms.gif) | ||
![IBM Cloud Pictograms](/images/IBM_Cloud_Pictograms.png) | ||
|
||
</GifPlayer> | ||
</Column> | ||
</Row> | ||
|
||
|
||
<Row> | ||
<Column colLg='8'> | ||
|
||
<Title>Dark</Title> | ||
|
||
<GifPlayer color='dark'> | ||
|
||
![IBM Cloud Platform Prototype](/images/IBM_Cloud_Platform_Prototype.gif) | ||
![IBM Cloud Platform Prototype](/images/IBM_Cloud_Platform_Prototype.png) | ||
|
||
</GifPlayer> | ||
</Column> | ||
</Row> | ||
|
||
## Code | ||
|
||
Place two images inside of the GifPlayer component. The first image will be used as the gif, the second image will be used as the static image on pause. Only provide two images inside the component, do not place any other children inside the component. | ||
|
||
<Title>Light</Title> | ||
|
||
```jsx path=components/GifPlayer/GifPlayer.js src= https://github.com/carbon-design-system/gatsby-theme-carbon/tree/master/packages/gatsby-theme-carbon/src/components/GifPlayer | ||
|
||
<Column colLg='4'> | ||
<GifPlayer> | ||
|
||
![IBM Cloud Pictograms](/images/IBM_Cloud_Pictograms.gif) // must be gif | ||
![IBM Cloud Pictograms](/images/IBM_Cloud_Pictograms.png) // must be static image | ||
|
||
</GifPlayer> | ||
</Column> | ||
|
||
``` | ||
|
||
<Title>Dark</Title> | ||
|
||
```jsx path=components/GifPlayer/GifPlayer.js src= https://github.com/carbon-design-system/gatsby-theme-carbon/tree/master/packages/gatsby-theme-carbon/src/components/GifPlayer | ||
|
||
<Column colLg='8'> | ||
<GifPlayer color='dark'> | ||
|
||
![IBM Cloud Platform Prototype](/images/IBM_Cloud_Platform_Prototype.gif) // must be gif | ||
![IBM Cloud Platform Prototype](/images/IBM_Cloud_Platform_Prototype.png) //must be static image | ||
|
||
</GifPlayer> | ||
</Column> | ||
|
||
``` | ||
|
||
### Props | ||
|
||
| property | propType | required | default | description | | ||
| --------- | -------- | -------- | ------- | --------------------- | | ||
| children | node | yes | | Pass in the images that will be rendered. Only pass in the images, no other children | | ||
| color | string | | `light` | Specify if the icon color should be light or dark | | ||
| className | string | | | Specify an optional className to be applied to the container node | |
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
Binary file added
BIN
+38.9 KB
packages/example/src/pages/components/images/IBM_Cloud_Pictograms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+107 KB
packages/example/src/pages/components/images/IBM_Cloud_Platform_Prototype.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
106 changes: 106 additions & 0 deletions
106
packages/gatsby-theme-carbon/src/components/GifPlayer/GifPlayer.js
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,106 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import { | ||
PlayOutline24, | ||
PlayOutlineFilled24, | ||
PauseOutline24, | ||
PauseOutlineFilled24, | ||
} from '@carbon/icons-react'; | ||
import styles from './GifPlayer.module.scss'; | ||
|
||
const Pause = ({ hovering }) => | ||
hovering ? <PauseOutlineFilled24 /> : <PauseOutline24 />; | ||
|
||
const Play = ({ hovering }) => | ||
hovering ? <PlayOutlineFilled24 /> : <PlayOutline24 />; | ||
|
||
const ToggleIcon = ({ paused, hovering }) => | ||
paused ? <Play hovering={hovering} /> : <Pause hovering={hovering} />; | ||
|
||
const GifPlayer = ({ children, color, className, isInDialog }) => { | ||
const [paused, setPaused] = useState(false); | ||
const [hovering, setHovering] = useState(false); | ||
|
||
const onClick = () => { | ||
setPaused(!paused); | ||
}; | ||
|
||
const controlsClassNames = classnames({ | ||
[styles.controls]: true, | ||
[styles.dark]: color === 'dark', | ||
}); | ||
|
||
const containerClassNames = classnames({ | ||
[styles.container]: true, | ||
[className]: className, | ||
[styles.gifInDialog]: isInDialog, | ||
}); | ||
|
||
const staticImageClassNames = classnames({ | ||
[styles.imgHidden]: true, | ||
[styles.imgDisplayed]: paused, | ||
}); | ||
|
||
const gifClassNames = classnames({ | ||
[styles.gifDisplayed]: true, | ||
[styles.gifHidden]: paused, | ||
}); | ||
|
||
const childrenArray = React.Children.toArray(children); | ||
|
||
const labelText = paused | ||
? 'Toggleable animation paused' | ||
: 'Toggleable animation playing'; | ||
|
||
return ( | ||
<div className={containerClassNames}> | ||
<div className={gifClassNames} aria-hidden={paused ? 'true' : false}> | ||
{childrenArray[0]} | ||
</div> | ||
<div | ||
className={staticImageClassNames} | ||
aria-hidden={paused ? false : 'true'} | ||
> | ||
{childrenArray[1]} | ||
</div> | ||
<button | ||
aria-pressed={paused ? 'true' : 'false'} | ||
type="button" | ||
aria-label={labelText} | ||
className={controlsClassNames} | ||
onMouseEnter={() => setHovering(true)} | ||
onMouseLeave={() => setHovering(false)} | ||
onClick={onClick} | ||
> | ||
<ToggleIcon hovering={hovering} paused={paused} /> | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
GifPlayer.propTypes = { | ||
/** | ||
* Specify if icon color should be "dark" or "light" | ||
*/ | ||
color: PropTypes.string, | ||
/** | ||
* Specify optional className | ||
*/ | ||
className: PropTypes.string, | ||
/** | ||
* Only pass in the 2 images to be rendered, first must be gif, second must be static image | ||
*/ | ||
children: PropTypes.arrayOf(PropTypes.element).isRequired, | ||
/** | ||
* Specify if the gifPlayer is inside the expanded ImageGallery (see ImageGallery.js) | ||
*/ | ||
isInDialog: PropTypes.bool, | ||
}; | ||
|
||
GifPlayer.defaultProps = { | ||
color: 'light', | ||
isInDialog: false, | ||
}; | ||
|
||
export default GifPlayer; |
52 changes: 52 additions & 0 deletions
52
packages/gatsby-theme-carbon/src/components/GifPlayer/GifPlayer.module.scss
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,52 @@ | ||
.container { | ||
position: relative; | ||
} | ||
|
||
.controls { | ||
position: absolute; | ||
bottom: 1rem; | ||
left: 1rem; | ||
height: 1.5rem; | ||
width: 1.5rem; | ||
background: transparent; | ||
outline: none; | ||
border: none; | ||
padding: 0; | ||
} | ||
|
||
.controls svg { | ||
fill: white; | ||
} | ||
|
||
.dark svg { | ||
fill: black; | ||
} | ||
|
||
.controls:focus svg { | ||
outline: 2px solid $focus; | ||
outline-offset: -2px; | ||
} | ||
|
||
//toggle static image | ||
.img-hidden { | ||
display: none; | ||
} | ||
|
||
.img-displayed { | ||
display: block; | ||
} | ||
|
||
//toggle gif | ||
.gif-displayed { | ||
display: block; | ||
} | ||
|
||
.gif-hidden { | ||
display: none; | ||
} | ||
|
||
//styles for gif player in image gallery | ||
.gif-in-dialog { | ||
top: 50%; | ||
transform: translateY(-50%); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/gatsby-theme-carbon/src/components/GifPlayer/index.js
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,3 @@ | ||
import GifPlayer from './GifPlayer'; | ||
|
||
export default GifPlayer; |
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
27d356c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: