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

HTML and CSS rework #72

Merged
merged 28 commits into from
Mar 15, 2022
Merged

Conversation

sergei-maertens
Copy link
Member

@sergei-maertens sergei-maertens commented Mar 14, 2022

The goal is to be less invasive and easier to theme, while cleaning up the actual CSS rules needed to make this work.

Tested against:

  • Django 2.2
  • Django 3.2
  • Django 4.0
  • Django 3.2 with Maykin default-project template

TODO:

  • Mobile styling (do we even want to bother? there's no hover state on mobile so we can't collapse/expand)
  • Make production CSS build instead of dev with sourcemaps
  • Regenerate package-lock.json with v2 format (use Node 16+)

Changes

  • Redid the CSS completely and properly isolated the dropdown-menu component
    • Removed almost all the hardcoded position: fixed and top: $magicNumber rules, replaced with position: sticky or even relative in a number of places
    • Properly leveraged flexbox to put the dropdown in the header while giving it 100% width
    • Use the CSS vars from Django 3.2 and used default values from 3.2 for pre 3.2 Django versions.
    • Added prefix to the CSS class names
  • Refactored the templates - now they extend from the django.contrib.admin templates, so they are now more compatible with different Django versions and less code for us to keep in sync with upstream
  • Dropped support for unsupported Django versions, only 2.2 LTS, 3.2 LTS and 4.0 are supported. It may still work on 3.0 and 3.1, but that's a problem for the library-user to solve now
  • Namespaced the inclusion template properly (Templates are not properly namespaced #69)
  • Added missing password change/done template overrides

Since we need to keep these templates in sync with Django's templates,
it's important we can easily diff them and code-reformatting is
detrimental for seeing actual meaningful changes.
For Django version pre 3.2, we define explicit fallback values equal
to the default values of the vars in Django 3.2
Position it sticky so that scrolling keeps it in the page, but without
absolute offsets.
* automatically gains the header bg color
* inherits the z-index from the header container
* removed the fixed/absolute positioning and hardcoded pixel offset
* properly namespace the CSS class names (known to conflict with
  bootstrap before!)
* Use the CSS variables for color information
* Lift styling rules to the item component and only override anchor
  styling where relevant
* Fix the header to display the dropdown menus (overflow)
* Hover and active state use the default admin CSS vars but can be
  overridden easily
* Moved CSS classes and styling to drop-item element rather than link
  itself
* The breadcrums are not position fixed/sticky anymore, however this
  should be easily overrideable in downstream projects.
@codecov
Copy link

codecov bot commented Mar 14, 2022

Codecov Report

Merging #72 (44e697d) into master (7cc1e11) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #72   +/-   ##
=======================================
  Coverage   98.78%   98.78%           
=======================================
  Files          10       10           
  Lines         246      246           
  Branches       46       46           
=======================================
  Hits          243      243           
  Misses          2        2           
  Partials        1        1           
Impacted Files Coverage Δ
django_admin_index/admin.py 100.00% <ø> (ø)
django_admin_index/models.py 100.00% <ø> (ø)
django_admin_index/apps.py 97.82% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7cc1e11...44e697d. Read the comment docs.

@sergei-maertens sergei-maertens marked this pull request as draft March 14, 2022 15:46
sergei-maertens and others added 5 commits March 14, 2022 18:44
Now they don't overflow beyond the viewport, but display a scrollbar.
The bar itself is ugly, but at least usable.
These were originally copied from the admin/css/base.css file, but only
a small subset are actually used by django-admin-index.
The default_auto_field should be specified. For the time being, we
keep the Django 3.2 default value (AutoField rather than
BigAutoField) and once only 3.2+ is supported, migrate to the new
Django BigAutoFiel default.
@sergei-maertens sergei-maertens force-pushed the feature/71-markup-and-css-rework branch from 3ac858c to 8fe0f43 Compare March 14, 2022 17:44
@sergei-maertens sergei-maertens force-pushed the feature/71-markup-and-css-rework branch from 8fe0f43 to b2bbeda Compare March 14, 2022 17:51
* password change form
* password change done
Django apps can inherit templates from other apps, so by putting
admin-index before djang.contrib.admin, you can inherit the django
admin default templates.

This allows us to only override the bits that need to be overridden,
making it easier to support multiple django versions and fixing/
applying the correct styling.
@sergei-maertens sergei-maertens force-pushed the feature/71-markup-and-css-rework branch from b2bbeda to 019d8eb Compare March 14, 2022 17:54
@joeribekker
Copy link
Member

I'm marked as reviewer but still draft?

Regarding your TODO's. For mobile styling we can disable the menu.

@sergei-maertens
Copy link
Member Author

yeah sorry, was a bit to eager with assigning a reviewer - there's a bit more tweaking to be done for easier overriding in default-project

@sergei-maertens sergei-maertens marked this pull request as ready for review March 14, 2022 22:19
@sergei-maertens
Copy link
Member Author

Ready for review now, and the accompanying PR for default-project is also prepared: https://bitbucket.org/maykinmedia/default-project/pull-requests/100

@JostCrow
Copy link
Contributor

@sergei-maertens If you want to support mobile only with CSS, it is possible to do that with :focus and using tabindex="0" on the HTML element. That way it is also more accessible.

@joeribekker
Copy link
Member

On second thought, it doesn't look that bad at the moment. With minimal effort, I think this actually works. Menus work since they are not links themselves.

So let's fix spacing and z index and go

@sergei-maertens
Copy link
Member Author

Alright, I'll give it a stab with those suggestions. Would indeed be nice if we have basic functionality working and then leave room for improvement with another approach for a different PR

@sergei-maertens
Copy link
Member Author

image

Good enough for now and tested on mobile

Used tabindex=0 to make the elements focusable, thanks @JostCrow for the
suggestion. The rest of the styling relies on the :focus pseudo-selector
and some alignment tweaks on mobile.
Copy link
Contributor

@svenvandescheur svenvandescheur left a comment

Choose a reason for hiding this comment

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

The combinatin of sass and css variables confuses me a lot. Can you elaborate on the choices made?

@@ -10,5 +10,8 @@ insert_final_newline = true
charset = utf-8
end_of_line = lf

[*.scss]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to change this?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, it's more in line with "big" mature projects like CRA. 2 spaces feels more natural than 4 tbh after working with both

scss/admin-index.scss Show resolved Hide resolved
@@ -7,6 +7,10 @@ $color-secondary: var(--secondary, #417690);
$color-accent: var(--accent, #f5dd5d);
$color-primary-fg: var(--primary-fg, #fff);

/* admin-index specific variables */
// default of 10px is the padding-bottom default of the header
$djai-dropdown-menu-spacing-top: var(--djai-dropdown-menu-spacing-top, 10px);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why we keep introducing variables while migrating to css variables? I think the combination is a bit confusing.

Copy link
Member Author

@sergei-maertens sergei-maertens Mar 15, 2022

Choose a reason for hiding this comment

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

I suppose in this case we could set the default CSS variable values, but the order of stylesheet inclusion (admin-index is loaded in the body!) makes overriding in your own custom stylesheet harder than it should be. E.g.:

<head>
    <link href="/static/admin/css/base.css">  <!-- contains :root with css vars -->
    <link href="/static/admin_overrides.css"> <!-- contains :root with css var overrides -->
</head>
<body>
    ...
    <div class"djai-dropdown-menu">
        <link href="/static/admin-index.css"> <!-- contains :root with css var defaults, which overrides the overrides again because of DOM order -->
    </div>
</body>

This way, the CSS var is used if it is defined, otherwise the default value fallback is used. And to not have to repeat the fallback value everywhere, I've opted to wrap that in a sass variable.

scss/_vars.scss Show resolved Hide resolved
@joeribekker
Copy link
Member

functional good enough for now.

@sergei-maertens sergei-maertens merged commit f85484a into master Mar 15, 2022
@sergei-maertens sergei-maertens deleted the feature/71-markup-and-css-rework branch March 15, 2022 10:40
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.

5 participants