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

Add "danger" and "note" CSS classes #115176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/doc/rustdoc/src/how-to-write-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ characters:

So, no need to manually enter those Unicode characters!

### Adding a warning block
### Adding a note/warning/danger block

If you want to make a warning or similar note stand out in the documentation,
you can wrap it like this:
Expand All @@ -267,6 +267,11 @@ you can wrap it like this:
/// more documentation
```

There are three levels available:
* note
* warning
* danger

[`backtrace`]: https://docs.rs/backtrace/0.3.50/backtrace/
[commonmark markdown specification]: https://commonmark.org/
[commonmark quick reference]: https://commonmark.org/help/
Expand Down
37 changes: 30 additions & 7 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,18 @@ ul ul, ol ul, ul ol, ol ol {
margin-bottom: .625em;
}

p, .docblock > .warning {
p, .docblock > .note, .docblock > .warning, .docblock > .danger {
/* Paragraph spacing at least 1.5 times line spacing per Web Content Accessibility Guidelines.
Line-height is 1.5rem, so line spacing is .5rem; .75em is 1.5 times that.
https://www.w3.org/WAI/WCAG21/Understanding/visual-presentation.html */
margin: 0 0 .75em 0;
}
/* For the last child of a div, the margin will be taken care of
by the margin-top of the next item. */
p:last-child, .docblock > .warning:last-child {
p:last-child,
.docblock > .note:last-child,
.docblock > .warning:last-child,
.docblock > .danger:last-child {
margin: 0;
}

Expand Down Expand Up @@ -1124,25 +1127,45 @@ pre.rust .doccomment {
font-size: 1.25rem;
}

/* This class only exists for users who want to draw attention to a particular element in their
/* These classes only exist for users who want to draw attention to a particular element in their
documentation. */
.content .docblock .warning {
border-left: 2px solid var(--warning-border-color);
.content .docblock .note,
.content .docblock .warning,
.content .docblock .danger {
padding: 14px;
position: relative;
/* The "!important" part is required because the rule is otherwise overruled in this CSS
selector: ".docblock > :not(.more-examples-toggle):not(.example-wrap)" */
overflow-x: visible !important;
}
.content .docblock .warning::before {
color: var(--warning-border-color);
.content .docblock .note::before,
.content .docblock .warning::before,
.content .docblock .danger::before {
content: "ⓘ";
position: absolute;
left: -25px;
top: 5px;
font-weight: bold;
font-size: 1.25rem;
}
.content .docblock .note {
border-left: 2px solid var(--note-border-color);
}
.content .docblock .warning {
border-left: 2px solid var(--warning-border-color);
}
.content .docblock .danger {
border-left: 2px solid var(--danger-border-color);
}
.content .docblock .note::before {
color: var(--note-border-color);
}
.content .docblock .warning::before {
color: var(--warning-border-color);
}
.content .docblock .danger::before {
color: var(--danger-border-color);
}

a.test-arrow {
visibility: hidden;
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/css/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Original by Dempfi (https://github.com/dempfi/ayu)
--codeblock-error-color: rgba(255, 0, 0, .5);
--codeblock-ignore-hover-color: rgb(255, 142, 0);
--codeblock-ignore-color: rgba(255, 142, 0, .6);
--warning-border-color: rgb(255, 142, 0);
--note-border-color: #00abff;
--warning-border-color: #ff8e00;
--danger-border-color: #ff2323;
--type-link-color: #ffa0a5;
--trait-link-color: #39afd7;
--assoc-item-link-color: #39afd7;
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/css/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
--codeblock-error-color: rgba(255, 0, 0, .5);
--codeblock-ignore-hover-color: rgb(255, 142, 0);
--codeblock-ignore-color: rgba(255, 142, 0, .6);
--warning-border-color: rgb(255, 142, 0);
--note-border-color: #0087ff;
--warning-border-color: #ff8e00;
--danger-border-color: #d22525;
--type-link-color: #2dbfb8;
--trait-link-color: #b78cf2;
--assoc-item-link-color: #d2991d;
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/css/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
--codeblock-error-color: rgba(255, 0, 0, .5);
--codeblock-ignore-hover-color: rgb(255, 142, 0);
--codeblock-ignore-color: rgba(255, 142, 0, .6);
--warning-border-color: rgb(255, 142, 0);
--note-border-color: #0087ff;
--warning-border-color: #ff8e00;
--danger-border-color: #d22525;
--type-link-color: #ad378a;
--trait-link-color: #6e4fc9;
--assoc-item-link-color: #3873ad;
Expand Down
4 changes: 4 additions & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ impl Foo {

/// hello
///
/// <div id="doc-note" class="note">this is a note</div>
///
/// <div id="doc-warning-1" class="warning">this is a warning</div>
///
/// <div id="doc-danger" class="danger">this is a danger</div>
///
/// done
pub fn warning1() {}

Expand Down
43 changes: 29 additions & 14 deletions tests/rustdoc-gui/warning-block.goml
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,57 @@ show-text: true

define-function: (
"check-warning",
(theme, color, border_color, background_color),
(theme, color, note_color, warning_color, danger_color),
block {
set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}
reload:

// The IDs are added directly into the DOM to make writing this test easier.
assert-css: ("#doc-note", {
"margin-bottom": "12px",
"color": |color|,
"border-left": "2px solid " + |note_color|,
"background-color": "transparent",
})
assert-css: ("#doc-warning-1", {
"margin-bottom": "12px",
"color": |color|,
"border-left": "2px solid " + |border_color|,
"background-color": |background_color|,
"border-left": "2px solid " + |warning_color|,
"background-color": "transparent",
})
assert-css: ("#doc-danger", {
"margin-bottom": "12px",
"color": |color|,
"border-left": "2px solid " + |danger_color|,
"background-color": "transparent",
})
assert-css: ("#doc-warning-2", {
"margin-bottom": "0px",
"color": |color|,
"border-left": "2px solid " + |border_color|,
"background-color": |background_color|,
"border-left": "2px solid " + |warning_color|,
"background-color": "transparent",
})
},
)

call-function: ("check-warning", {
"theme": "ayu",
"color": "rgb(197, 197, 197)",
"border_color": "rgb(255, 142, 0)",
"background_color": "rgba(0, 0, 0, 0)",
"color": "#c5c5c5",
"note_color": "#00abff",
"warning_color": "#ff8e00",
"danger_color": "#ff2323",
})
call-function: ("check-warning", {
"theme": "dark",
"color": "rgb(221, 221, 221)",
"border_color": "rgb(255, 142, 0)",
"background_color": "rgba(0, 0, 0, 0)",
"color": "#ddd",
"note_color": "#0087ff",
"warning_color": "#ff8e00",
"danger_color": "#d22525",
})
call-function: ("check-warning", {
"theme": "light",
"color": "rgb(0, 0, 0)",
"border_color": "rgb(255, 142, 0)",
"background_color": "rgba(0, 0, 0, 0)",
"color": "black",
"note_color": "#0087ff",
"warning_color": "#ff8e00",
"danger_color": "#d22525",
})
Loading