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

Customization via CSS variables #282

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

zanderle
Copy link
Collaborator

This PR makes the notifications a bit more reliant on the CSS variables. I tried doing the same for search as well, but focused mostly on colors. Making the full design rely on font-size is a bit more tricky for the search modal, because there's more involved than the "simple box" that are the notifications. We can always improve as we move further.

I also had to change the setting of the default CSS variable on flyout from :root to :host as the former didn't seem to work.

Closes #197

In order to make the notifications box customizable when
it comes to font-size, we need to ensure other aspects
(like paddings, line-heights, etc) are changed accordingly
as well.
…an :root

For some reason, defining the initial CSS variable on :root doesn't work.
The easy workaround (which also makes it a bit more flexible) is to
define it on :host instead.
@zanderle zanderle requested review from humitos and a team as code owners March 29, 2024 13:13
@zanderle zanderle requested a review from agjohnson March 29, 2024 13:13
@humitos
Copy link
Member

humitos commented Apr 8, 2024

Making the full design rely on font-size is a bit more tricky for the search modal, because there's more involved than the "simple box" that are the notifications

Should we have a specific variable for this? I don't know, maybe readthedocs-<addons>-size: 100% where all the size/spacing/etc are calculated based on that variable? Would a variable like this make this easier?

@zanderle
Copy link
Collaborator Author

zanderle commented Apr 8, 2024

Making the full design rely on font-size is a bit more tricky for the search modal, because there's more involved than the "simple box" that are the notifications

Should we have a specific variable for this? I don't know, maybe readthedocs-<addons>-size: 100% where all the size/spacing/etc are calculated based on that variable? Would a variable like this make this easier?

That's a good idea. But no, it's more a matter of going through every single design element in the search modal, determining how it should relate to the font-size (or some kind of a global variable) and making sure it works well with it. It's just a time-consuming process more than anything else.
If you think it's an important feature to have, I can still add that.

Copy link
Contributor

@agjohnson agjohnson left a comment

Choose a reason for hiding this comment

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

All of this seems good. It is indeed a shame there isn't a way to reference the root shadow dom element using an absolute unit like rem.

@humitos
Copy link
Member

humitos commented Apr 11, 2024

I will try to test this locally soon and merge after that.

@humitos
Copy link
Member

humitos commented Apr 16, 2024

@zanderle @agjohnson how are you testing this? I added the following CSS and I'm not seeing any change:

:host {
  --readthedocs-flyout-font-size: 0.2rem;
  --readthedocs-notification-font-size: 0.2rem;
  --readthedocs-search-font-size: 0.2rem;
}

@agjohnson
Copy link
Contributor

Did you try setting those variables on <html> or something top level and outside the shadow DOM?

@humitos
Copy link
Member

humitos commented Apr 17, 2024

Using html seems to work:

html {
--readthedocs-flyout-font-size: 0.63rem;  /* Material for MkDocs */
}

@agjohnson Is this the correct way we want users to customize the sizes?

One thing I noticed here is the branch icon doesn't scale accordingly with this setting:

Screenshot_2024-04-17_12-16-06

I found this because the size of this icon is bigger on Material for MkDocs than on Sphinx by default (same as the font), but changing the --readthedocs-flyout-font-size doesn't make the icon to scale accordingly. @zanderle Is it possible to consider this as well?

@zanderle
Copy link
Collaborator Author

Using html seems to work:

html {
--readthedocs-flyout-font-size: 0.63rem;  /* Material for MkDocs */
}

@agjohnson Is this the correct way we want users to customize the sizes?

You can also set it on body or on the web-component itself, and it should work the same.

One thing I noticed here is the branch icon doesn't scale accordingly with this setting:

Screenshot_2024-04-17_12-16-06

I found this because the size of this icon is bigger on Material for MkDocs than on Sphinx by default (same as the font), but changing the --readthedocs-flyout-font-size doesn't make the icon to scale accordingly. @zanderle Is it possible to consider this as well?

Yes, I'll add that. Good catch.

@humitos
Copy link
Member

humitos commented Apr 17, 2024

@zanderle can you tell me what are the steps you are following to test this? I'm having issues with. I'm not sure why sometimes it works and sometimes it doesn't. I may not be following the correct steps here.

@zanderle
Copy link
Collaborator Author

@zanderle can you tell me what are the steps you are following to test this? I'm having issues with. I'm not sure why sometimes it works and sometimes it doesn't. I may not be following the correct steps here.

When I'm testing, I do it in developer tools, and create the variable on the element itself:

Screenshot 2024-04-17 at 16 17 46

@humitos
Copy link
Member

humitos commented Apr 17, 2024

OK, yeah. Testing it that way it works for me as well (and the SVG icon scales properly 👍🏼 ). However, it's still not clear to me how users should define these variables. We talked about defining them under :host, :root, html and/or body. However, none of them work for me now. @zanderle do they work for you?

@agjohnson how do we want users to define these variables? Can you provide an example about this?

@humitos
Copy link
Member

humitos commented Apr 17, 2024

I tried doing the same for search as well, but focused mostly on colors. Making the full design rely on font-size is a bit more tricky for the search modal, because there's more involved than the "simple box" that are the notifications. We can always improve as we move further.

I opened #292 to track and work on this in a following PR 👍🏼

@zanderle
Copy link
Collaborator Author

OK, yeah. Testing it that way it works for me as well (and the SVG icon scales properly 👍🏼 ). However, it's still not clear to me how users should define these variables. We talked about defining them under :host, :root, html and/or body. However, none of them work for me now. @zanderle do they work for you?

Yeah, I'm noticing it now too. Not sure about the "sometimes it works, sometimes it doesn't", but it looks like the css variable defined in the web-component using :host { } takes precedence over anything defined in the parent DOM (even with !important), since that's the scope it's in. However if we don't define it using :host { } we can't provide the default value for the variable.
The way around this is that we just have to define it in a way that's more specific than :host. So in the parent DOM we could either have

/* Using the element */
readthedocs-flyout {
  --readthedocs-flyout-font-size: 1.2rem;
}

or if we defined the webcomponent to have a specific class, we could do:

/* Using the class */
.some-specific-class {
  --readthedocs-flyout-font-size: 1.2rem;
}

...

<readthedocs-flyout class="some-specific-class"></readthedocs-flyout>

If that's not acceptable, I'll have to do some more research to see if I'm missing something

@agjohnson
Copy link
Contributor

agjohnson commented Apr 17, 2024

So, all of these options should operate the same, they are all valid methods.

However, our variables are scoped per-addon (--readthedocs-flyout-font-size) so I think our guidance should be setting the variables at the top level. At the parent, the html or :root selector are most common.

@agjohnson how do we want users to define these variables? Can you provide an example about this?

There isn't really a standard to point at here. MDN docs show :root, you'll commonly see vars on html too though. :root is just html with higher specificity.

However if we don't define it using :host { } we can't provide the default value for the variable.

There are two ways I see us getting around this:

First, we can use an internal variable name that doesn't conflict with the outer variable name. Inside the flyout CSS for example:

:host {
  --readthedocs-inner-flyout-font-size: var(--readthedocs-flyout-font-size, 0.8rem);
}

This seems easy and low friction, I didn't test this though and maybe I'm wrong here.


Maybe a larger wrench in our plans than we wanted is a different version of the above though.

At the parent level, users could do any of these:

:root {
  --readthedocs-font-size: 19px;
}

readthedocs-flyout {
  --readthedocs-font-size: 20px;
}

readthedocs-flyout.bigger {
  --readthedocs-flyout-font-size: 21px;
}

And our addons set the variables as:

:host {
  --readthedocs-flyout-font-size: var(--readthedocs-font-size, 0.8rem);
}

p {
  font-size: var(--readthedocs-flyout-font-size);
}

Giving us shared CSS variables at html/:root/whatever and individual addons variables at the addon element specificity. Or combine the two so that users aren't thrown off setting --readthedocs-flyout-font-size at :root.

@zanderle
Copy link
Collaborator Author

There are two ways I see us getting around this:

First, we can use an internal variable name that doesn't conflict with the outer variable name. Inside the flyout CSS for example:

:host {
  --readthedocs-inner-flyout-font-size: var(--readthedocs-flyout-font-size, 0.8rem);
}

This seems easy and low friction, I didn't test this though and maybe I'm wrong here.

Yeah, this one should be pretty easy to do - it would just be a matter of renaming the "inner" variables.

Maybe a larger wrench in our plans than we wanted is a different version of the above though.

At the parent level, users could do any of these:

:root {
  --readthedocs-font-size: 19px;
}

readthedocs-flyout {
  --readthedocs-font-size: 20px;
}

readthedocs-flyout.bigger {
  --readthedocs-flyout-font-size: 21px;
}

And our addons set the variables as:

:host {
  --readthedocs-flyout-font-size: var(--readthedocs-font-size, 0.8rem);
}

p {
  font-size: var(--readthedocs-flyout-font-size);
}

Giving us shared CSS variables at html/:root/whatever and individual addons variables at the addon element specificity. Or combine the two so that users aren't thrown off setting --readthedocs-flyout-font-size at :root.

Yeah, I think the combination would be a good approach too. I'll try putting this together to see if there are any hidden problems and then we can decide on the final "format".

Allow for a general --readthedocs-font-size, but make it easy to
override the specific addon with --readthedocs-<addon>-font-size
@zanderle
Copy link
Collaborator Author

I wasn't sure about the naming, but take a look at the last commit in this PR. How do you feel about this approach? @humitos @agjohnson

@humitos
Copy link
Member

humitos commented May 14, 2024

I wasn't sure about the naming, but take a look at the last commit in this PR. How do you feel about this approach?

Looks good to me 👍🏼

@agjohnson
Copy link
Contributor

It looks like --addons-* is the internal variable name that we manage, and --readthedocs-* is the external, user controlled variable name?

@humitos you are generally a proponent of keeping the --readthedocs-* prefix the same on all variables, does having the two competing prefixes seem okay to you?

Otherwise this approach looks great to me.

@humitos
Copy link
Member

humitos commented May 22, 2024

I'm not too worried about the internal name. We can always change it later if we find a better one. However, I want to keep --readthedocs-* prefix for all variables we are exposing to users.

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.

Customization via CSS variables
3 participants