Skip to content

Commit d080e7c

Browse files
authored
Merge pull request #561 from primer/add-hidden-docs
Add HTML hidden attribute docs, increase [hidden] selector specificity
2 parents d070918 + 913ae29 commit d080e7c

File tree

4 files changed

+741
-3206
lines changed

4 files changed

+741
-3206
lines changed

modules/primer-base/lib/base.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ button {
6868
border-radius: 0;
6969
}
7070

71+
// increase the selector specificity for [hidden]
72+
// so that it always overrides utility classes (.d-block, etc.)
73+
[hidden][hidden] {
74+
display: none !important;
75+
}
76+
7177
details {
7278
summary { cursor: pointer; }
7379

modules/primer-utilities/docs/layout.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ Adjust the display of an element with `.d-block`, `.d-none`, `.d-inline`, `.d-in
3131
</div>
3232
```
3333

34+
### The HTML `hidden` attribute
35+
36+
As of [Primer v10.10.0](https://github.com/primer/primer/releases/v10.10.0), `primer-base` includes a rule that sets `display: none !important` for any element with the [`hidden` attribute][hidden]. You can safely use the `hidden` attribute with display utilities, but we suggest:
37+
38+
1. Use the `hidden` attribute (and corresponding JavaScript property) if you're going to programmatically show and hide content.
39+
1. Use `d-none` and/or its responsive variants (`d-sm-block`, `d-lg-none`) to conditionally show content at different screen sizes.
40+
41+
Rather than toggling the `d-none` class in JavaScript, you should toggle the `hidden` property on an element. This means that you won't have to restore any more specific display utility (`d-inline` or `d-flex`, for instance) just to work around the order in which they're listed in the stylesheet.
42+
43+
```js
44+
// Good:
45+
element.hidden = !visible
46+
47+
// Bad:
48+
element.classList.toggle('d-none', !visible)
49+
element.classList.toggle('d-inline', visible)
50+
```
51+
52+
### `display:table` wrapping issues
53+
3454
There are known issues with using `display:table` and wrapping long strings, particularly in Firefox. You may need to use `table-fixed` on elements with `d-table` and apply column widths to table cells, which you can do with our [column width styles](../../objects/grid#column-widths).
3555

3656
```html
@@ -298,3 +318,5 @@ Create a double-sided media object for a container with a flexible center.
298318
```
299319

300320
You can also create a media object with [flexbox utilities](./flexbox#media-object) instead of floats which can be useful for changing the vertical alignment.
321+
322+
[hidden]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden

modules/primer-utilities/lib/visibility-display.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ $display-values: (
2222
}
2323
}
2424

25-
// Visibility utilities
26-
/* Visibility hidden */
2725
.v-hidden { visibility: hidden !important; }
28-
/* Visibility visible */
2926
.v-visible { visibility: visible !important; }
3027

3128
// Hide utilities for each breakpoint

0 commit comments

Comments
 (0)