This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b6db45
commit 0506256
Showing
4 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
name: Duration | ||
menu: Components | ||
--- | ||
|
||
import {Playground, Props} from 'docz'; | ||
import Duration from './'; | ||
|
||
# Duration | ||
|
||
A simple component for rendering a duration (e.g. of a video) in the format `m:s` or `h:m:s` with an accessible label. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
## Examples | ||
|
||
<Playground> | ||
<Duration value={27} /> | ||
<br /> | ||
<Duration value={523} /> | ||
<br /> | ||
<Duration value={1104} /> | ||
<br /> | ||
<Duration value={7329} /> | ||
</Playground> | ||
|
||
## Props | ||
|
||
<Props of={Duration} /> |
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,21 @@ | ||
function getZeroPaddedRest(value, base) { | ||
const remaining = value % base; | ||
if (remaining < 10) { | ||
return String(remaining).padStart(2, '0'); | ||
} else return remaining; | ||
} | ||
|
||
function getDuration(durationInSeconds) { | ||
const hours = parseInt(durationInSeconds / (60 * 60)); | ||
let minutes = parseInt(durationInSeconds / 60); | ||
minutes = hours ? getZeroPaddedRest(minutes, 60) : minutes; | ||
const seconds = getZeroPaddedRest(durationInSeconds, 60); | ||
|
||
return { | ||
hours, | ||
minutes, | ||
seconds, | ||
}; | ||
} | ||
|
||
export default getDuration; |
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,46 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import getDuration from './getDuration'; | ||
|
||
function getDefaultLabel(minutes, hours) { | ||
const addPluralS = count => (count === 1 ? '' : 's'); | ||
|
||
const readableMins = `${minutes} minute${addPluralS(minutes)}`; | ||
This comment has been minimized.
Sorry, something went wrong.
ikhemissi
Contributor
|
||
if (hours) { | ||
return `${hours} hour${addPluralS(minutes)}, ${readableMins}`; | ||
} else return readableMins; | ||
} | ||
|
||
function Duration(props) { | ||
const {value, getLabel, as: Component, ...otherProps} = props; | ||
const {hours, minutes, seconds} = getDuration(value); | ||
|
||
const duration = hours ? [hours, minutes, seconds] : [minutes, seconds]; | ||
|
||
return ( | ||
<Component | ||
{...otherProps} | ||
aria-label={getLabel(Number(minutes), Number(hours))} | ||
> | ||
{duration.join(':')} | ||
</Component> | ||
); | ||
} | ||
|
||
Duration.defaultProps = { | ||
as: 'span', | ||
getLabel: getDefaultLabel, | ||
}; | ||
|
||
Duration.propTypes = { | ||
/** Change the element rendered by the component */ | ||
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]), | ||
/** Customise the accessible label, i.e. for localisation */ | ||
getLabel: PropTypes.func, | ||
/** Duration in seconds */ | ||
value: PropTypes.number.isRequired, | ||
}; | ||
|
||
export {getDuration}; | ||
|
||
export default Duration; |
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
Can you please add a line here to say that the component accepts a numerical value (number of seconds) as input ?