Commit 02047ae4 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Add `aria-expanded` to excerpt show/hide toggles

parent a813d4e4
...@@ -31,10 +31,6 @@ export default function AnnotationBody({ ...@@ -31,10 +31,6 @@ export default function AnnotationBody({
const [isCollapsible, setIsCollapsible] = useState(false); const [isCollapsible, setIsCollapsible] = useState(false);
const toggleText = isCollapsed ? 'More' : 'Less'; const toggleText = isCollapsed ? 'More' : 'Less';
const toggleTitle = isCollapsed
? 'Show full annotation text'
: 'Show the first few lines only';
const showExcerpt = !isEditing && text.length > 0; const showExcerpt = !isEditing && text.length > 0;
const showTagList = !isEditing && tags.length > 0; const showTagList = !isEditing && tags.length > 0;
...@@ -69,10 +65,11 @@ export default function AnnotationBody({ ...@@ -69,10 +65,11 @@ export default function AnnotationBody({
{isCollapsible && !isEditing && ( {isCollapsible && !isEditing && (
<div className="annotation-body__collapse-toggle"> <div className="annotation-body__collapse-toggle">
<Button <Button
buttonText={toggleText}
className="annotation-body__collapse-toggle-button" className="annotation-body__collapse-toggle-button"
isExpanded={!isCollapsed}
onClick={() => setIsCollapsed(!isCollapsed)} onClick={() => setIsCollapsed(!isCollapsed)}
buttonText={toggleText} title="Toggle to show full annotation text"
title={toggleTitle}
/> />
</div> </div>
)} )}
......
...@@ -21,6 +21,7 @@ export default function Button({ ...@@ -21,6 +21,7 @@ export default function Button({
className = '', className = '',
disabled = false, disabled = false,
icon = '', icon = '',
isExpanded,
isPressed, isPressed,
onClick = () => null, onClick = () => null,
style = {}, style = {},
...@@ -40,6 +41,9 @@ export default function Button({ ...@@ -40,6 +41,9 @@ export default function Button({
// Indicate that this is a toggle button. // Indicate that this is a toggle button.
extraProps['aria-pressed'] = isPressed; extraProps['aria-pressed'] = isPressed;
} }
if (typeof isExpanded === 'boolean') {
extraProps['aria-expanded'] = isExpanded;
}
return ( return (
<button <button
...@@ -110,6 +114,11 @@ Button.propTypes = { ...@@ -110,6 +114,11 @@ Button.propTypes = {
*/ */
icon: requiredStringIfButtonTextMissing, icon: requiredStringIfButtonTextMissing,
/**
* Is the expandable element controlled by this button currently expanded?
*/
isExpanded: propTypes.bool,
/** /**
* Indicate that this is a toggle button (if `isPressed` is a boolean) and * Indicate that this is a toggle button (if `isPressed` is a boolean) and
* whether it is pressed. * whether it is pressed.
......
...@@ -20,7 +20,8 @@ function InlineControls({ isCollapsed, setCollapsed, linkStyle = {} }) { ...@@ -20,7 +20,8 @@ function InlineControls({ isCollapsed, setCollapsed, linkStyle = {} }) {
<button <button
className="excerpt__toggle-button" className="excerpt__toggle-button"
onClick={() => setCollapsed(!isCollapsed)} onClick={() => setCollapsed(!isCollapsed)}
aria-label="Toggle to show the full excerpt or first few lines only" aria-expanded={!isCollapsed}
aria-label="Toggle to show the full excerpt"
aria-pressed={(!isCollapsed).toString()} aria-pressed={(!isCollapsed).toString()}
style={linkStyle} style={linkStyle}
> >
......
...@@ -65,7 +65,8 @@ describe('AnnotationBody', () => { ...@@ -65,7 +65,8 @@ describe('AnnotationBody', () => {
const button = wrapper.find('Button'); const button = wrapper.find('Button');
assert.isOk(button.exists()); assert.isOk(button.exists());
assert.equal(button.props().buttonText, 'More'); assert.equal(button.props().buttonText, 'More');
assert.equal(button.props().title, 'Show full annotation text'); assert.equal(button.props().title, 'Toggle to show full annotation text');
assert.isFalse(button.props().isExpanded);
}); });
it('shows appropriate button text to collapse the Excerpt if expanded', () => { it('shows appropriate button text to collapse the Excerpt if expanded', () => {
...@@ -87,7 +88,8 @@ describe('AnnotationBody', () => { ...@@ -87,7 +88,8 @@ describe('AnnotationBody', () => {
const buttonProps = wrapper.find('Button').props(); const buttonProps = wrapper.find('Button').props();
assert.equal(buttonProps.buttonText, 'Less'); assert.equal(buttonProps.buttonText, 'Less');
assert.equal(buttonProps.title, 'Show the first few lines only'); assert.equal(buttonProps.title, 'Toggle to show full annotation text');
assert.isTrue(buttonProps.isExpanded);
}); });
describe('tag list and editor', () => { describe('tag list and editor', () => {
......
...@@ -41,6 +41,18 @@ describe('Button', () => { ...@@ -41,6 +41,18 @@ describe('Button', () => {
assert.equal(wrapper.find('SvgIcon').prop('name'), 'fakeIcon'); assert.equal(wrapper.find('SvgIcon').prop('name'), 'fakeIcon');
}); });
[true, false].forEach(isExpanded => {
it('sets `aria-expanded` attribute if `isExpanded` is a boolean', () => {
const wrapper = createComponent({ isExpanded });
assert.equal(wrapper.find('button').prop('aria-expanded'), isExpanded);
});
});
it('does not set `aria-expanded` attribute if `isExpanded` is omitted', () => {
const wrapper = createComponent();
assert.notProperty(wrapper.find('button').props(), 'aria-expanded');
});
[true, false].forEach(isPressed => { [true, false].forEach(isPressed => {
it('sets `aria-pressed` attribute if `isPressed` is a boolean', () => { it('sets `aria-pressed` attribute if `isPressed` is a boolean', () => {
const wrapper = createComponent({ isPressed }); const wrapper = createComponent({ isPressed });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment