Skip to content

Commit

Permalink
Mim 1932 sr arrow keyfigure (#1196)
Browse files Browse the repository at this point in the history
* Add screen reader only text for key figures change variation
Add aria-label attribute for glossary since children prop can be a node

MIM-1932

* Bump ssb-component library from 2.2.2 to 2.2.3

* Update key figures test with screen reader text changes

* Minor code refactoring

* Remove unused screen import

* Update key figures storybook examples with sr only text example
  • Loading branch information
johnnadeluy authored Jul 26, 2024
1 parent f140d75 commit f127a08
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 79 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statisticsnorway/ssb-component-library",
"version": "2.2.2",
"version": "2.2.3",
"description": "Component library for SSB (Statistics Norway)",
"main": "lib/bundle.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/components/Glossary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ Available props:
| className | string | Optional container class |
| closeText | string | Close button text, default "Lukk" |
| explanation | required string | Text to be inside popup |
| ariaLabel | string | Defines a string value that describes the glossary |
5 changes: 3 additions & 2 deletions src/components/Glossary/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState, useRef, useCallback } from 'react'
import PropTypes from 'prop-types'
import { BookOpen, XCircle } from 'react-feather'

const Glossary = ({ explanation, children, className, closeText }) => {
const Glossary = ({ explanation, children, className, closeText, ariaLabel }) => {
const node = useRef()
const [open, setOpen] = useState(false)

Expand Down Expand Up @@ -51,7 +51,7 @@ const Glossary = ({ explanation, children, className, closeText }) => {
ref={node}
onClick={() => setOpen(!open)}
className='glossary-button'
aria-label={children}
aria-label={ariaLabel || children}
aria-expanded={open ? 'true' : 'false'}
>
<span className='glossary-text-wrap'>{children}</span>
Expand Down Expand Up @@ -86,6 +86,7 @@ Glossary.propTypes = {
className: PropTypes.string,
closeText: PropTypes.string,
explanation: PropTypes.string.isRequired,
ariaLabel: PropTypes.string,
}

export default Glossary
2 changes: 1 addition & 1 deletion src/components/KeyFigures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Available props:

| Name | Type | Description |
| ----------------- | --------------------------------------------------------------------- | -------------------------------------------------------- |
| changes | object (changeDirection [up, down or same], changeText, changePeriod) | Renders changetext with icon |
| changes | object (changeDirection [up, down or same], changeText, changePeriod, srChangeText) | Renders changetext with icon |
| className | string | Optional container class |
| icon | node | Renders an icon |
| number | string or number | Large number to be displayed |
Expand Down
47 changes: 44 additions & 3 deletions src/components/KeyFigures/__snapshots__/keyFigures.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports[`KeyFigures component Matches the snapshot 1`] = `
>
<button
aria-expanded="false"
aria-label="[object Object]"
aria-label="Antall husholdninger"
class="glossary-button"
>
<span
Expand Down Expand Up @@ -140,6 +140,19 @@ exports[`KeyFigures component Matches the snapshot 1`] = `
husholdninger
</span>
</div>
<div
class="kf-changes"
>
<span
aria-hidden="false"
class="changes-text"
/>
 
<span
aria-hidden="false"
class="changes-periode"
/>
</div>
</div>
</div>
</DocumentFragment>
Expand Down Expand Up @@ -173,6 +186,19 @@ exports[`KeyFigures component Render green box variation 1`] = `
class="kf-title subtitle"
/>
</div>
<div
class="kf-changes"
>
<span
aria-hidden="false"
class="changes-text"
/>
 
<span
aria-hidden="false"
class="changes-periode"
/>
</div>
</div>
</div>
</DocumentFragment>
Expand All @@ -189,15 +215,17 @@ exports[`KeyFigures component Renders Glossary 1`] = `
>
<button
aria-expanded="false"
aria-label="[object Object]"
aria-label="Antall husholdninger"
class="glossary-button"
>
<span
class="glossary-text-wrap"
>
<span
class="kf-title"
/>
>
Antall husholdninger
</span>
</span>
<svg
aria-hidden="true"
Expand Down Expand Up @@ -289,6 +317,19 @@ exports[`KeyFigures component Renders Glossary 1`] = `
class="kf-title subtitle"
/>
</div>
<div
class="kf-changes"
>
<span
aria-hidden="false"
class="changes-text"
/>
 
<span
aria-hidden="false"
class="changes-periode"
/>
</div>
</div>
</div>
</DocumentFragment>
Expand Down
76 changes: 41 additions & 35 deletions src/components/KeyFigures/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,65 @@ import Number from '../Number'
import Glossary from '../Glossary'

const KeyFigures = ({
changes,
changes = {},
className,
icon,
number,
numberDescription,
noNumberText,
noNumberText = 'Tall ikke tilgjengelig',
size,
title,
time,
glossary,
greenBox,
}) => (
<div className={`ssb-key-figures ${size}${greenBox ? ' green-box' : ''}${className ? ` ${className}` : ''}`}>
{icon && <div className={`kf-icon ${size}`}>{icon}</div>}
<div>
{glossary ? (
<Glossary explanation={glossary}>
}) => {
const { changeDirection, changeText, changePeriod, srChangeText } = changes
return (
<div className={`ssb-key-figures ${size}${greenBox ? ' green-box' : ''}${className ? ` ${className}` : ''}`}>
{icon && <div className={`kf-icon ${size}`}>{icon}</div>}
<div>
{glossary && title ? (
<Glossary explanation={glossary} ariaLabel={title}>
<span className='kf-title'>{title}</span>
</Glossary>
) : (
<span className='kf-title'>{title}</span>
</Glossary>
) : (
<span className='kf-title'>{title}</span>
)}
<div className='kf-time'>{time}</div>
{number ? (
<div className='number-section'>
<Number size={size}>{number}</Number>
<span className='kf-title subtitle'>{numberDescription}</span>
</div>
) : (
<span className='no-number'>{noNumberText}</span>
)}
{changes && (
<div className='kf-changes'>
{changes.changeDirection === 'up' && <ArrowUp className='changes-icon' size={20} />}
{changes.changeDirection === 'down' && <ArrowDown className='changes-icon' size={20} />}
{changes.changeDirection === 'same' && <Minus className='changes-icon' size={20} />}
<span className='changes-text'>{changes.changeText}</span>&nbsp;
<span className='changes-periode'>{changes.changePeriod}</span>
</div>
)}
)}
<div className='kf-time'>{time}</div>
{number ? (
<div className='number-section'>
<Number size={size}>{number}</Number>
<span className='kf-title subtitle'>{numberDescription}</span>
</div>
) : (
<span className='no-number'>{noNumberText}</span>
)}
{changes && (
<div className='kf-changes'>
{changeDirection === 'up' && <ArrowUp className='changes-icon' size={20} aria-hidden='true' />}
{changeDirection === 'down' && <ArrowDown className='changes-icon' size={20} aria-hidden='true' />}
{changeDirection === 'same' && <Minus className='changes-icon' size={20} aria-hidden='true' />}
<span className='changes-text' aria-hidden={srChangeText ? 'true' : 'false'}>
{changeText}
</span>
&nbsp;
<span className='changes-periode' aria-hidden={srChangeText ? 'true' : 'false'}>
{changePeriod}
</span>
{srChangeText ? <span className='sr-only'>{srChangeText}</span> : null}
</div>
)}
</div>
</div>
</div>
)

KeyFigures.defaultProps = {
noNumberText: 'Tall ikke tilgjengelig',
)
}

KeyFigures.propTypes = {
changes: PropTypes.shape({
changeDirection: PropTypes.oneOf(['up', 'down', 'same']),
changeText: PropTypes.string,
changePeriod: PropTypes.string,
srChangeText: PropTypes.string,
}),
className: PropTypes.string,
icon: PropTypes.node,
Expand Down
52 changes: 26 additions & 26 deletions src/components/KeyFigures/keyFigures.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const Large = () => (
numberDescription='husholdninger'
time='2018'
size='large'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
glossary={placeholderText}
/>
</div>
Expand All @@ -33,7 +33,7 @@ export const Medium = () => (
numberDescription='husholdninger'
time='2018'
size='medium'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
/>
</div>
)
Expand All @@ -46,7 +46,7 @@ export const Small = () => (
numberDescription='plasser'
time='2018'
size='small'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
/>
</div>
)
Expand Down Expand Up @@ -76,24 +76,16 @@ export const WithoutIcon = () => (
</div>
)

WithoutIcon.story = {
name: 'Without icon',
}

export const WithoutNumber = () => (
<KeyFigures
title='Antall plasser i helse- og omsorgsinstitusjoner'
numberDescription='plasser'
time='2018'
size='small'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
/>
)

WithoutNumber.story = {
name: 'Without number',
}

export const WithChanges = () => (
<div>
<KeyFigures
Expand All @@ -102,7 +94,7 @@ export const WithChanges = () => (
numberDescription='husholdninger'
time='2018'
size='large'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
glossary={placeholderText}
changes={{
changeDirection: 'up',
Expand All @@ -120,7 +112,7 @@ export const WithChanges = () => (
numberDescription='husholdninger'
time='2018'
size='medium'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
changes={{
changeDirection: 'same',
changeText: 'Ingen endring',
Expand All @@ -137,7 +129,7 @@ export const WithChanges = () => (
numberDescription='plasser'
time='2018'
size='small'
icon={<HouseIcon alt='house' />}
icon={<HouseIcon />}
changes={{
changeDirection: 'down',
changeText: 'Ned 1 prosentpoeng',
Expand All @@ -147,9 +139,25 @@ export const WithChanges = () => (
</div>
)

WithChanges.story = {
name: 'With changes',
}
export const WithChangesAndScreenReaderOnlyText = () => (
<div>
<KeyFigures
number='789 398'
title={title}
numberDescription='husholdninger'
time='2018'
size='large'
icon={<HouseIcon />}
glossary={placeholderText}
changes={{
changeDirection: 'up',
changeText: '1,5%',
changePeriod: 'fra året før',
srChangeText: 'Oppgang 1,5% fra året før',
}}
/>
</div>
)

export const GreenboxVariasjonMedium = () => (
<KeyFigures
Expand All @@ -162,10 +170,6 @@ export const GreenboxVariasjonMedium = () => (
/>
)

GreenboxVariasjonMedium.story = {
name: 'Greenbox variasjon medium',
}

export const GreenboxVariasjonStor = () => (
<KeyFigures
title='Valgdeltagelse ved stortingsvalg'
Expand All @@ -176,7 +180,3 @@ export const GreenboxVariasjonStor = () => (
greenBox
/>
)

GreenboxVariasjonStor.story = {
name: 'Greenbox variasjon stor',
}
Loading

0 comments on commit f127a08

Please sign in to comment.