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 expand/collapse-all button for API doc details #920

Merged
merged 6 commits into from
Jun 5, 2024
Merged
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
2 changes: 2 additions & 0 deletions docs/_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="https://code.iconify.design/iconify-icon/1.0.7/iconify-icon.min.js"></script>
<link href="{{root}}{{fsdocs-favicon-src}}" rel="icon" sizes="32x32" type="image/png"/>
<script type="application/javascript" src="{{root}}content/fsdocs-theme-set-dark.js"></script>
<script type="application/javascript" src="{{root}}content/fsdocs-details-set-expanded.js"></script>
<link href="{{root}}content/fsdocs-default.css" rel="stylesheet" type="text/css"/>
<link href="{{root}}content/fsdocs-theme.css" rel="stylesheet" type="text/css"/>
{{fsdocs-head-extra}}
Expand Down Expand Up @@ -93,6 +94,7 @@
</dialog>
<script type="module" src="{{root}}content/fsdocs-tips.js"></script>
<script type="module" src="{{root}}content/fsdocs-theme-toggle.js"></script>
<script type="module" src="{{root}}content/fsdocs-details-toggle.js"></script>
<script type="module" src="{{root}}content/fsdocs-theme.js"></script>
<script type="module" src="{{root}}content/fsdocs-search.js"></script>
{{fsdocs-body-extra}}
Expand Down
7 changes: 7 additions & 0 deletions docs/content/fsdocs-default.css
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,13 @@ span[onmouseout] {
margin-top: 0;
}

.fsdocs-member-list-header {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
}

.fsdocs-xmldoc {
& pre {
overflow-x: auto;
Expand Down
9 changes: 9 additions & 0 deletions docs/content/fsdocs-details-set-expanded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const expandDetails = !!JSON.parse(localStorage.getItem('details-expanded'));

addEventListener('load', _ => {
if (expandDetails) {
for (const details of document.getElementsByTagName('details')) {
details.setAttribute('open', 'true');
}
}
});
46 changes: 46 additions & 0 deletions docs/content/fsdocs-details-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';

const detailsExpanded = !!JSON.parse(localStorage.getItem('details-expanded'));

export class DetailsToggle extends LitElement {
static properties = {
_detailsExpanded: { state: true, type: Boolean },
};

constructor() {
super();
this._detailsExpanded = detailsExpanded;
document.addEventListener('detailstoggled', e => this._detailsExpanded = !!e?.detail?.expanding);
}

static styles = css`
:host {
cursor: pointer;
}
`;

toggleDetails() {
this._detailsExpanded = !this._detailsExpanded;
localStorage.setItem('details-expanded', JSON.stringify(this._detailsExpanded));
if (this._detailsExpanded) {
for (const details of document.getElementsByTagName('details')) {
details.setAttribute('open', 'true');
}
} else {
for (const details of document.getElementsByTagName('details')) {
details.removeAttribute('open');
}
}
document.dispatchEvent(new CustomEvent('detailstoggled', { detail: { expanding: this._detailsExpanded } }));
}

render() {
const icon = this._detailsExpanded ? 'carbon:collapse-categories' : 'carbon:expand-categories';
const title = this._detailsExpanded ? 'Collapse details' : 'Expand details';
return html`
<iconify-icon width="24" height="24" icon="${icon}" title="${title}" style="color:var(--header-link-color)" @click=${this.toggleDetails}></iconify-icon>
`;
}
}

customElements.define('fsdocs-details-toggle', DetailsToggle);
2 changes: 1 addition & 1 deletion src/FSharp.Formatting.ApiDocs/GenerateHtml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type HtmlRender(model: ApiDocModel, ?menuTemplateFolder: string) =
thead [] [
tr [] [
td [ Class "fsdocs-member-list-header" ] [ !!tableHeader ]
td [ Class "fsdocs-member-list-header" ] [ !! "Description" ]
td [ Class "fsdocs-member-list-header" ] [ !! "Description"; fsdocsDetailsToggle [] ]
]
]
tbody [] [
Expand Down
4 changes: 4 additions & 0 deletions src/FSharp.Formatting.Common/HtmlModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,7 @@ module internal Html =
/// Web component from https://iconify.design/docs/
let iconifyIcon (props: HtmlProperties list) =
HtmlElement.CustomElement("iconify-icon", props, [])

/// Toggle button for API doc details.
let fsdocsDetailsToggle (props: HtmlProperties list) =
HtmlElement.CustomElement("fsdocs-details-toggle", props, [])
Loading