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

Fixed #35189 -- Render admin collapsible fieldsets with <details>. #17910

Merged
merged 4 commits into from
May 22, 2024

Conversation

MHLut
Copy link
Contributor

@MHLut MHLut commented Feb 26, 2024

TL;DR: Improve the accessibility of admin fieldsets and collapse them with a native HTML implementation.

Related tickets and PRs

Related items:

Important facts

  • Works with the main model fieldset and stacked inline fieldsets.
  • This change removes all JavaScript for collapsible fieldsets.
  • I have recycled as much CSS as possible.
  • JavaScript used to check if a fieldset could be collapsible. With JS gone, I moved that check to the Fieldset helper class in Python.
  • The translations for "Show" and "Hide" are now redundant.

Known issues

  • No existing CSS bugs were fixed.
    • Example: The font size of the fieldset is below 16px/1rem.
    • Example: The fieldset heading is hard to distinguish in forced-colors mode.
    • Context: Style for .collapse. h3 in forms.css was copied from .module h2, .module caption, .inline-group h2 in base.css.

Note: Several CSS fixes that we could make would cause scope creep beyond the collapsible fieldsets, so I recommend fixing them in separate PRs.

@carltongibson
Copy link
Member

carltongibson commented Feb 26, 2024

buildbot, test on selenium. I don’t think that works anymore 😅

@carltongibson carltongibson added the selenium Apply to have Selenium tests run on a PR label Feb 26, 2024
@knyghty knyghty requested a review from a team February 26, 2024 14:58
@MHLut

This comment was marked as outdated.

@MHLut MHLut marked this pull request as ready for review March 1, 2024 10:55
@MHLut

This comment was marked as outdated.

@MHLut MHLut force-pushed the ticket_35189 branch 2 times, most recently from 627eb66 to ffb0a66 Compare March 4, 2024 13:52
@MHLut MHLut marked this pull request as draft March 4, 2024 14:50
@MHLut MHLut marked this pull request as ready for review March 4, 2024 14:51
@MHLut

This comment was marked as resolved.

@MHLut MHLut changed the title Fixed #35189 -- Made admin collapsible fieldsets render in <details> tag. Fixed #35189 -- Render admin collapsible fieldsets with <details>. Mar 11, 2024
@sarahboyce
Copy link
Contributor

sarahboyce commented Apr 12, 2024

Hi @MHLut I think this looks really promising thank you ⭐
I've found something I'd like you to look into. I had some screenshots generated from this PR. You can see these here when you download the Artifacts.
These screenshots are taken under a few scenarios we try to support include dark mode and right to left languages (side note we should think about adding forced-colors mode to these).

I can see on your PR that in RTL we have this (notice that 'Collapsible' is on the left)
test_selectbox_height_collapsible_fieldset_rtl-selectbox-collapsible

Previously 'Collapsible' was on the right:
test_selectbox_height_collapsible_fieldset_rtl-selectbox-collapsible

I am also not sure if the collapse icon (> triangle thingy) should be on the left, so before Fields in the right to left case (maybe @sakhawy might be able to tell us 🙏 )

You might need to look into django/contrib/admin/static/admin/css/rtl.css 👍

@sarahboyce
Copy link
Contributor

(on further thought, I think the triangle is in the correct place for RTL languages but Moe is welcome to confirm)

@sakhawy
Copy link
Contributor

sakhawy commented Apr 12, 2024

@sarahboyce I guess it "feels off" because we're used to seeing dropdowns with the triangle after the text and not before it :D
But I think the triangle here is part of the <details> tag's default style, so nothing to worry about!

One reason for checking the django/contrib/admin/static/admin/css/rtl.css file would be this line:

I think it's the reason 'Collapsible' is on the left when dir="rtl".

@MHLut MHLut force-pushed the ticket_35189 branch 2 times, most recently from 69349a0 to 81f1b78 Compare April 15, 2024 14:13
@MHLut
Copy link
Contributor Author

MHLut commented Apr 15, 2024

@sarahboyce @sakhawy Thanks for the heads-up. I can't remember why I put the text-align: left in forms.py, but it's gone now. The heading aligns to the right side again in RTL mode.

The triangle is—as Moe mentioned—part of <details> native styling; I'd rather not mess with that. Since Moe seems OK with it, I'll keep that as-is.

FYI: I'm currently doing some free-form CSS experiments for forced-colors mode.

@sarahboyce sarahboyce added selenium Apply to have Selenium tests run on a PR and removed selenium Apply to have Selenium tests run on a PR labels Apr 15, 2024
Copy link
Member

@knyghty knyghty left a comment

Choose a reason for hiding this comment

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

I didn't look much at the code, or look at it on a screen.

From an accessibility standpoint I think we're good with this approach. The outline seems a little odd still (that there are multiple h3s per link, rather than one h3 and then going to h4 or something similar) but I think this is better than what we currently have and shouldn't block this merge - to be honest I'm not even sure if it's a problem at all, I could be totally wrong.

If I find time before someone else I will also review the code.

@MHLut
Copy link
Contributor Author

MHLut commented May 14, 2024

@nessita The tests finally seem to pass, so that you can return to testing and reviewing!

I don't have much time for follow-ups, but I would like to see this merged before the feature freeze, so feel free to update the branch as you see fit.

@sakhawy might also be able to assist with issues that don't require knowing the entire PR history.

@nessita
Copy link
Contributor

nessita commented May 14, 2024

@nessita The tests finally seem to pass, so that you can return to testing and reviewing!

I don't have much time for follow-ups, but I would like to see this merged before the feature freeze, so feel free to update the branch as you see fit.

@sakhawy might also be able to assist with issues that don't require knowing the entire PR history.

Thank you so much @MHLut, I'll work in this PR tomorrow or Thu at the latest.

@nessita
Copy link
Contributor

nessita commented May 17, 2024

@MHLut Hey there! I have pushed some changes removing the definition and usage of heading_id because it felt inconsistent with the existing code, and also because it's usually a bad practice to rely on random numbers to generate unique IDs (random module does not guarantee uniqueness, even in large ranges). We could use UUID instead (which does guarantee uniqueness) but that feels a bit overkill for the branch's need, so I built the fieldset heading ID by using information available in the template (or its parent template), similar to what formsets already do.

@sarahboyce Would you fancy to re-review that commit (it's isolated in a second commit to ease review)? (Since I changed some of the already approved code.)

FYI I still need to review in detail tests and docs, but overall I think the branch is in very good shape.

Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

I didn't do a full review since the change of approach. 👍
This generally looks good, we're missing tests checking the logic that exists in the templates

docs/ref/contrib/admin/index.txt Outdated Show resolved Hide resolved
django/contrib/admin/helpers.py Show resolved Hide resolved
@MHLut
Copy link
Contributor Author

MHLut commented May 18, 2024

@nessita @sarahboyce Thank you for the heading ID update! I cannot review the ID changes in detail right now, but the only thing important to me is that they are predictable and unique since aria-labelledby relies on them for accessibility reasons.

A nice to have for IDs would be for them to be short and readable so that they are understandable when read from a browser URL.

Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

Looks good!
If we want to split commits I would have:

  • setting the header levels and adding the aria-labelledby as "Associated fieldsets with their headers."
  • updating collapse to use details/summary

django/contrib/admin/static/admin/css/forms.css Outdated Show resolved Hide resolved
tests/admin_inlines/tests.py Outdated Show resolved Hide resolved
tests/admin_inlines/tests.py Outdated Show resolved Hide resolved
tests/admin_inlines/tests.py Outdated Show resolved Hide resolved
docs/releases/5.1.txt Outdated Show resolved Hide resolved
@nessita
Copy link
Contributor

nessita commented May 21, 2024

Looks good! If we want to split commits I would have:

* setting the header levels and adding the `aria-labelledby` as "Associated fieldsets with their headers."

* updating collapse to use details/summary

This is a good proposal, thank you! I will try to work this out after our weekly sync.

…a-labelledby.

Before this change, HTML <fieldset> elements in the admin site did not
have an associated label to describe them. This commit defines a unique
HTML id for the heading labeling a fieldset, and sets its
aria-labelledby property to link the heading with the fieldset.
@nessita
Copy link
Contributor

nessita commented May 22, 2024

I split the first commit into two logical commits.

MHLut and others added 3 commits May 21, 2024 22:52
…s> elements.

This work improves the accessibility of the add and change pages in the
admin site by adding <details> and <summary> elements to the collapsible
fieldsets. This has the nice side effect of no longer requiring custom
JavaScript helpers to implement the fieldsets' show/hide capabilities.

Thanks to James Scholes for the accessibility advice, and to Sarah Boyce
and Tom Carrick for reviews.

Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
@nessita nessita merged commit 7857507 into django:main May 22, 2024
35 checks passed
@MHLut MHLut deleted the ticket_35189 branch May 23, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Djangonauts 🚀 screenshots 🖼️ selenium Apply to have Selenium tests run on a PR
9 participants