Skip to content

Use component siblings custom hook#611

Merged
imobachgs merged 7 commits intoagama-project:masterfrom
balsa-asanovic:use-node-siblings-hook
Jun 21, 2023
Merged

Use component siblings custom hook#611
imobachgs merged 7 commits intoagama-project:masterfrom
balsa-asanovic:use-node-siblings-hook

Conversation

@balsa-asanovic
Copy link
Copy Markdown
Contributor

Problem

In the Sidebar component an internal function to access its siblings was introduced.
This pull request address issue #565 by creating a custom hook for this purpose and using it instead in Sidebar component.

fixes #565

Solution

The logic of getting HTML node's siblings is moved to a custom hook, where also the functions are introduced for adding and removing attributes to sibling elements. The hook returns these functions as an array. The hook takes an HTML element as input parameter in form of useRef.current.

Testing

At the moment I haven't added any unit tests for the hook, before I get some feedback on the code changes I am not sure if the approach I made is good at all.

I tested the functionality in the app, this works, I can confirm that the inert and aria attributes are being added and removed appropriately.

Screenshots

image

@coveralls
Copy link
Copy Markdown

coveralls commented Jun 13, 2023

Pull Request Test Coverage Report for Build 5332205595

  • 21 of 21 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 76.83%

Totals Coverage Status
Change from base Build 5322890036: 0.03%
Covered Lines: 5644
Relevant Lines: 7102

💛 - Coveralls

Copy link
Copy Markdown
Contributor

@dgdavid dgdavid left a comment

Choose a reason for hiding this comment

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

First and foremost, thanks a ton for taking care of this ❤️

We apologize for the delay in reviewing it; we've been busy with the changes for the June release.

That said, your changes look in the right direction. I left a few comments, but feel free to just add the testing part and ping us again for merging it to master branch as soon as all checks are green.

Do not hesitate to contact us if you have questions about unit testing or filling out the changes file.

}, [isOpen]);
if (isOpen) {
closeButtonRef.current.focus();
makeSiblingsInert();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch 👍 As it is now, it will not become siblings as inert if the component is mounted as isOpen in the future (for whatever reason, I have no use case in mind). But your change fixes it.

Comment on lines +9 to +11
if (!node) return [
(a, b) => { return [a, b] },
(a) => { return [a] }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's ok, but maybe I'd return noop functions instead. Or even skip that check and make siblings an empty array when node is not defined yet.

No strong opinion at this moment, honestly.

Comment on lines +1 to +7
/**
* Returns two functions, one which adds given attributes to siblings
* of the node passed as the hook parameter, and another one which removes attributes.
*
* @param {HTMLElement} node
* @returns {Array}
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a suggestion,

Suggested change
/**
* Returns two functions, one which adds given attributes to siblings
* of the node passed as the hook parameter, and another one which removes attributes.
*
* @param {HTMLElement} node
* @returns {Array}
*/
/**
* Function for adding an attribute to a sibling
*
* @typedef {function} addAttributeFn
* @param {string} attribute - attribute name
* @param {*} value - value to set
*/
/**
* Function for removing an attribute from a sibling
*
* @typedef {function} removeAttributeFn
* @param {string} attribute - attribute name
*/
/**
* A hook for working with siblings of the node passed as parameter
*
* It returns an array with exactly two functions:
* - First for adding given attribute to siblings
* - Second for removing given attributes from siblings
*
* @param {HTMLElement} node
* @returns {[addAttributeFn, removeAttributeFn]}
*/

@dgdavid dgdavid requested a review from imobachgs June 13, 2023 14:52
@balsa-asanovic
Copy link
Copy Markdown
Contributor Author

Hey @dgdavid

Thanks for the review and for the suggestions.

I addressed them in the code and made commits accordingly.

I've also added unit tests now for the custom hook.

Do tell me if anything else is needed for the PR to get merged.
I've noticed you mentioned "filling out the changes file", but I am not sure what to do here, so any help is appreciated.

Copy link
Copy Markdown
Contributor

@imobachgs imobachgs left a comment

Choose a reason for hiding this comment

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

Thanks @balsa-asanovic! The only missing piece is adding an entry to the changes file. I would suggest something like this (but feel free to use a different text):

-------------------------------------------------------------------
Wed Jun 21 05:27:53 UTC 2023 - Balsa Asanovic <balsaasanovic95@gmail.com>

- Move the logic to add/remove sibling elements attributes
  to a custom hook (gh#openSUSE/agama#565).

@balsa-asanovic
Copy link
Copy Markdown
Contributor Author

Hey @imobachgs

Thanks for pointing me to the right file.

I've added short description of changes, I hope it's OK.

@imobachgs
Copy link
Copy Markdown
Contributor

Thank you! It looks OK. I will merge the PRs as soon as the CI runs.

@imobachgs imobachgs merged commit a820318 into agama-project:master Jun 21, 2023
@imobachgs
Copy link
Copy Markdown
Contributor

The code is finally merged. @balsa-asanovic Thanks a lot for your contribution!

@balsa-asanovic
Copy link
Copy Markdown
Contributor Author

Great!

Thank you guys a lot for the help and guidance. @dgdavid @imobachgs

I'll look now for another issue to work on 😃

@balsa-asanovic balsa-asanovic deleted the use-node-siblings-hook branch June 21, 2023 18:38
@imobachgs imobachgs mentioned this pull request Aug 2, 2023
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.

Create a hook to work with siblings elements

4 participants