Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ make HdIcon accessible #481

Merged
merged 7 commits into from
Jul 20, 2020
Merged

✨ make HdIcon accessible #481

merged 7 commits into from
Jul 20, 2020

Conversation

majakomel
Copy link
Contributor

Hi!

This PR improves HdIcon accessibility as suggested in #414
Changes follow the tips on accessible SVGs from the linked article - the title and desc tags are added to the SVG, along with aria-labelledby and role attributes.

Looking forward to the review 🙂

Copy link
Contributor

@ilyasmez ilyasmez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @majakomel,

Congrats on your first PR 🎉
And thank you for tackling one of our open issues, we really appreciate it 😃

I've dropped some comments, please check them and ask questions if you have any.

const descId = `${this.id || randId}-desc`;
const descTag = document.createElementNS('http://www.w3.org/2000/svg', 'desc');
descTag.id = descId;
svg.appendChild(descTag);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using appendChild here and insetBefore for the titleTag, any reason for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's recommended that the title is the first child because of backward compatibility with SVG 1.1 (https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title). It doesn't seem to matter for desc.

Copy link
Contributor

@viniciuskneves viniciuskneves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @majakomel

Thanks for submitting the PR, nice job!

I saw that @ilyasmez already went through most of the code so I will just add my 2 cents here 😅

Comment on lines 69 to 77
const randId = Math.floor(Math.random() * 1000);
if (this.title) {
const titleId = `${this.id || randId}-title`;
const titleTag = document.createElementNS('http://www.w3.org/2000/svg', 'title');
titleTag.textContent = this.title;
titleTag.id = titleId;
svg.insertBefore(titleTag, svg.firstChild);
svg.setAttribute('aria-labelledby', titleId);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you to split title and description into separated functions just as a matter of structuring the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can use if like that, this practice may be more readable

if (this.title) return false;
const randId = Math.floor(Math.random() * 1000);
const titleId = `${this.id || randId}-title`;
const titleTag = document.createElementNS('http://www.w3.org/2000/svg', 'title');
titleTag.textContent = this.title;
titleTag.id = titleId;
svg.insertBefore(titleTag, svg.firstChild);
svg.setAttribute('aria-labelledby', titleId);

Comment on lines +88 to +101
it('adds title and desc tag to the svg', async () => {
const wrapper = wrapperBuilder({
props: {
src: 'fake/icon4.svg',
title: 'This make me accessible.',
description: 'Some description.',
id: 'icon-id',
},
});

await wrapper.vm.$nextTick();

expect(wrapper.html()).toMatchSnapshot();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you to add a test to cover the "random id" case as well.

@majakomel
Copy link
Contributor Author

Sorry for the delay and thanks for suggestions @viniciuskneves & @volcanioo, I reshuffled the code a bit, looks better separated into 2 methods :)

Comment on lines 47 to 49
const randId = Math.floor(Math.random() * 1000);
if (this.title) this.addTitleTag(svg, randId);
if (this.description) this.addDescTag(svg, randId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehhh, I think the idea of addAccessibilityTags is valid. There I would call the other methods 🙈
Sorry 😅

Comment on lines 103 to 114
it("generates the title and desc id if it's not provided", async () => {
const wrapper = wrapperBuilder({
props: {
src: 'fake/icon5.svg',
title: 'This make me accessible.',
description: 'Some description.',
},
});

await wrapper.vm.$nextTick();

expect(wrapper.html()).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 114 to 117
const titleTag = wrapper.find('title')
const descTag = wrapper.find('desc')
expect(titleTag.element.id).toMatch(/\d+-title/)
expect(descTag.element.id).toMatch(/\d+-desc/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, the random is biting here, right? 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it didn't fail in the first run :D

Copy link
Contributor

@viniciuskneves viniciuskneves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Copy link
Contributor

@ilyasmez ilyasmez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Maja :)

@ilyasmez ilyasmez merged commit 661fe3e into homeday-de:develop Jul 20, 2020
viniciuskneves added a commit that referenced this pull request Jul 22, 2020
# Changelog

## Minor
- ✨ Support title and description in HdIcon (#481)
- ✨ Forward attributes from HdInput to <input> (#483)

## Patch
- ⬆️ Bump lodash from 4.17.15 to 4.17.19 (#484)
- ⬆️ Node v10.22.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants