Skip to content

Commit 25f4776

Browse files
committed
new usage block
1 parent 78faa81 commit 25f4776

File tree

9 files changed

+80
-29
lines changed

9 files changed

+80
-29
lines changed

build_css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import $ from "https://deno.land/x/dax@0.36.0/mod.ts";
2-
import browserslist from "npm:browserslist@4.22.2";
2+
import browserslist from "npm:browserslist@4.23.0";
33
import { browserslistToTargets, transform } from "npm:lightningcss";
44

55
const browsers = browserslist(">= 0.5%, not dead");

src/html/comrak_adapters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl HighlightAdapter {
5050
));
5151
}
5252

53-
highlighter(&mut lines, line)?;
53+
highlighter(&mut lines, &html_escape::encode_text(line))?;
5454

5555
if self.show_line_numbers {
5656
lines.push_str("</span>");

src/html/jsdoc.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,11 @@ pub fn markdown_to_html(
295295

296296
let mut html = {
297297
let arena = Arena::new();
298-
let root = comrak::parse_document(&arena, md, &options);
298+
let root = comrak::parse_document(&arena, &md, &options);
299299

300300
walk_node(&arena, root, &options, &plugins);
301301

302-
let mut bw = std::io::BufWriter::new(Vec::new());
303-
comrak::format_html_with_plugins(root, &options, &mut bw, &plugins)
304-
.unwrap();
305-
String::from_utf8(bw.into_inner().unwrap()).unwrap()
302+
render_node(root, &options, &plugins)
306303
};
307304

308305
#[cfg(feature = "ammonia")]

src/html/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ pub type UsageComposer = Rc<
6262
&RenderContext,
6363
&[DocNodeWithContext],
6464
String,
65-
) -> IndexMap<String, String>,
65+
) -> IndexMap<UsageComposerEntry, String>,
6666
>;
6767

68+
#[derive(Eq, PartialEq, Hash)]
69+
pub struct UsageComposerEntry {
70+
pub name: String,
71+
pub icon: Option<std::borrow::Cow<'static, str>>,
72+
}
73+
6874
#[derive(Clone)]
6975
pub struct GenerateOptions {
7076
/// The name that is shown is the top-left corner, eg. "deno_std".

src/html/templates/script.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
function findParent(el, find) {
2+
do {
3+
if (find(el)) {
4+
return el;
5+
}
6+
} while (el = el.parentElement);
7+
}
8+
19
document.addEventListener("click", (e) => {
2-
if (e.target instanceof HTMLButtonElement && e.target.dataset["copy"]) {
3-
return navigator?.clipboard?.writeText(e.target.dataset["copy"]);
10+
const target = findParent(e.target, (el) => el instanceof HTMLButtonElement && el.dataset["copy"]);
11+
if (target) {
12+
navigator?.clipboard?.writeText(target.dataset["copy"]);
413
}
514
});
15+
16+
window.addEventListener("load", () => {
17+
const checkbox = document.getElementById("usageDropdownInput");
18+
document.addEventListener("mouseup", (e) => {
19+
const label = findParent(e.target, (el) => el instanceof HTMLLabelElement && el.htmlFor === "usageDropdownInput");
20+
if(!label) {
21+
checkbox.checked = false;
22+
}
23+
});
24+
});

src/html/templates/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ summary::-webkit-details-marker {
7979
}
8080

8181
.usage pre.highlight {
82-
@apply border border-gray-300;
82+
@apply border border-gray-300 max-md:border-x-0;
8383
}
8484

8585
/* markdown */

src/html/templates/styles.gen.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/html/templates/usages.hbs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="p-4 mb-4 border-2 rounded-lg bg-stone-50 border-stone-200 usage">
1+
<div class="px-6 py-5 mb-4 border max-md:border-x-0 md:rounded-lg bg-stone-50 border-stone-200 usage">
22
{{~#each usages~}}
33
<style scoped>
44
{{{~additional_css~}}} {{! css }}
@@ -7,13 +7,40 @@
77
<input type="radio" name="usage" id="{{name}}" class="hidden" {{~#if @first~}}checked{{~/if~}} />
88
{{~/each~}}
99

10-
<nav class="{{~#unless ../show_tabs~}}hidden{{~/unless}} border-b border-gray-300 flex flex-row mb-2">
11-
{{~#each usages~}}
12-
<label for="{{name}}" class="cursor-pointer select-none md:px-4 px-4 py-1 text-sm md:text-base leading-none rounded-t-md md:hover:bg-gray-100 md:hover:border-b-2">
13-
{{name}}
14-
</label>
15-
{{~/each~}}
16-
</nav>
10+
{{~#if ../show_tabs~}}
11+
<nav class="flex items-center flex-row gap-2 mb-3 font-semibold">
12+
<span>Usage instructions for</span>
13+
14+
<div>
15+
<input type="checkbox" id="usageDropdownInput" class="hidden peer" />
16+
17+
<label for="usageDropdownInput" class="flex gap-1 select-none cursor-pointer">
18+
{{~#each usages~}}
19+
<div id="{{name}}_active_dropdown" class="hidden items-center gap-1">
20+
<div class="h-4 *:h-4 *:w-auto">{{{icon}}} {{! icon }}</div>
21+
<div class="leading-none">{{name}}</div>
22+
</div>
23+
{{~/each~}}
24+
25+
<div class="rotate-90">
26+
{{~> icons/arrow }}
27+
</div>
28+
</label>
29+
30+
<div class="md:relative hidden peer-checked:block" id="usageDropdown">
31+
<div class="absolute max-md:inset-x-0 mt-1.5 p-2 block z-30 md:w-72 bg-white md:rounded border max-md:border-x-0 border-gray-300">
32+
{{~#each usages~}}
33+
<label for="{{name}}" class="flex items-center justify-between cursor-pointer select-none px-2 py-1 leading-normal rounded-sm hover:bg-gray-50">
34+
<div>{{name}}</div>
35+
36+
<div class="h-5 *:h-5 *:w-auto">{{{icon}}} {{! icon }}</div>
37+
</label>
38+
{{~/each~}}
39+
</div>
40+
</div>
41+
</div>
42+
</nav>
43+
{{~/if}}
1744

1845
<div>
1946
{{~#each usages~}}

src/html/usage.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ fn render_css_for_usage(name: &str) -> String {
1010
#{name}:checked ~ *:last-child > :not(#{name}_content) {{
1111
display: none;
1212
}}
13-
#{name}:checked ~ nav:first-of-type > label[for='{name}'] {{
14-
border-bottom-width: 2px;
15-
cursor: unset;
16-
border-color: rgb(17 24 39);
13+
#{name}:checked ~ nav #{name}_active_dropdown {{
14+
display: flex;
1715
}}
18-
#{name}:not:checked ~ nav:first-of-type > label[for='{name}'] {{
19-
border-color: rgb(209 213 219);
16+
#{name}:checked ~ nav label[for='{name}'] {{
17+
cursor: unset;
18+
background-color: #EBF6FF;
2019
}}
2120
"#
2221
)
@@ -112,6 +111,7 @@ pub struct UsagesCtx {
112111
pub struct UsageCtx {
113112
name: String,
114113
content: String,
114+
icon: Option<std::borrow::Cow<'static, str>>,
115115
additional_css: String,
116116
}
117117

@@ -130,9 +130,10 @@ impl UsagesCtx {
130130

131131
let usages = usages
132132
.into_iter()
133-
.map(|(name, content)| UsageCtx {
134-
additional_css: render_css_for_usage(&name),
135-
name,
133+
.map(|(entry, content)| UsageCtx {
134+
additional_css: render_css_for_usage(&entry.name),
135+
name: entry.name,
136+
icon: entry.icon,
136137
content: crate::html::jsdoc::render_markdown(ctx, &content),
137138
})
138139
.collect::<Vec<_>>();
@@ -155,6 +156,7 @@ impl UsagesCtx {
155156
usages: vec![UsageCtx {
156157
name: "".to_string(),
157158
content: rendered_import_statement,
159+
icon: None,
158160
additional_css: "".to_string(),
159161
}],
160162
})

0 commit comments

Comments
 (0)