Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/four-snakes-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"hyperbook-simple-template": minor
---

The sections can now be opened and closed. The active page will always
start opened. For each section you can overwrite the default expanded
state by adding `expanded: true` to the frontmatter of the section.
5 changes: 5 additions & 0 deletions .changeset/polite-scissors-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperbook/element-collapsible": minor
---

The Element Collapsible now has a little animation when opening and closing.
3 changes: 2 additions & 1 deletion packages/element-collapsible/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dependencies": {
"@hyperbook/provider": "workspace:*",
"@hyperbook/store": "workspace:*",
"object-hash": "3.0.0"
"object-hash": "3.0.0",
"react-collapsed": "^3.4.0"
},
"peerDependencies": {
"react": "18.x",
Expand Down
11 changes: 5 additions & 6 deletions packages/element-collapsible/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
padding: 8px 16px;
transition: 0.3s;
border-radius: 4px;
margin-bottom: 10px;
}

.element-collapsible.button + .element-collapsible.content {
margin-top: -10px;
}

.element-collapsible.button.active {
Expand All @@ -29,15 +34,9 @@

.element-collapsible.content {
border-top: none;
max-height: 0;
overflow: hidden;
margin-bottom: 10px;
}

.element-collapsible.content.active {
max-height: 100%;
}

.element-collapsible .inner {
border-style: solid;
border-width: 1px;
Expand Down
16 changes: 11 additions & 5 deletions packages/element-collapsible/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDispatch, useSelector } from "react-redux";
import { createSlice, PayloadAction } from "@hyperbook/store";
import { useActivePageId } from "@hyperbook/provider";
import "./index.css";
import useCollapse from "react-collapsed";

type DirectiveCollapsibleProps = {
children?: ReactNode;
Expand All @@ -15,15 +16,18 @@ const DirectiveCollapsible: FC<DirectiveCollapsibleProps> = ({
id,
title,
}) => {
const [activePageId] = useActivePageId();

if (!id) {
id = title;
}

const [activePageId] = useActivePageId();
id = activePageId + "." + id;

let active = useSelector(selectActive(id));
const { getCollapseProps, getToggleProps } = useCollapse({
isExpanded: active,
});

const dispatch = useDispatch();

const toggleActive = () => {
Expand All @@ -33,17 +37,19 @@ const DirectiveCollapsible: FC<DirectiveCollapsibleProps> = ({
return (
<>
<button
id={`collapsibel-${id}`}
{...getToggleProps({
onClick: () => toggleActive(),
})}
className={[
"element-collapsible",
"button",
active ? "active" : "",
].join(" ")}
onClick={() => toggleActive()}
>
{title}
</button>
<div
{...getCollapseProps()}
className={[
"element-collapsible",
"content",
Expand All @@ -56,7 +62,7 @@ const DirectiveCollapsible: FC<DirectiveCollapsibleProps> = ({
);
};

type ElementCollapsibleState = Record<string, string>;
type ElementCollapsibleState = Record<string, boolean>;

const initialState: ElementCollapsibleState = {};

Expand Down
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions templates/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"gray-matter": "4.0.3",
"next": "12.3.1",
"react": "18.2.0",
"react-collapsed": "^3.4.0",
"react-dom": "18.2.0",
"react-icons": "4.4.0",
"react-redux": "8.0.4",
Expand Down
2 changes: 1 addition & 1 deletion templates/simple/src/colors.css
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ header.inverted > .branding {
border-color: var(--color-spacer);
}

.section > .name.empty {
.section .name.empty {
color: var(--color-text-deactivated);
}

Expand Down
39 changes: 29 additions & 10 deletions templates/simple/src/components/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from "next/link";
import useCollapse from "react-collapsed";
import {
Navigation as NavigationProps,
Section as SectionProps,
Expand Down Expand Up @@ -28,20 +29,38 @@ const Section = ({
href,
pages,
sections,
expanded,
current,
}: S) => {
const isActive = current?.href.startsWith(href);
const { getCollapseProps, getToggleProps, isExpanded } = useCollapse({
defaultExpanded: isActive || expanded,
});
return (
<div className={virtual ? "" : "section"}>
{virtual ? null : isEmpty ? (
<span className="name empty">{name}</span>
) : (
<Link href={href}>
<a className={current?.href === href ? `name active` : "name"}>
{name}
</a>
</Link>
)}
<div className="links">
<div
className={[
"name",
virtual || isEmpty ? "empty" : "",
current?.href === href ? "active" : "",
].join(" ")}
>
{virtual ? null : isEmpty ? (
<span className="label">{name}</span>
) : (
<Link href={href}>
<a className="label">{name}</a>
</Link>
)}
<button
className="toggle"
{...getToggleProps()}
aria-label={isExpanded ? "Close" : "Open"}
>
{isExpanded ? "➖" : "➕"}
</button>
</div>
<div className="links" {...getCollapseProps()}>
{pages.length > 0 && (
<ul className="pages">
{pages
Expand Down
12 changes: 12 additions & 0 deletions templates/simple/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ nav li + li {
}

.section > .name {
display: flex;
text-decoration: none;
padding: 10px;
border-width: 1px;
Expand All @@ -263,6 +264,17 @@ nav li + li {
user-select: none;
}

.section > .name > .label {
flex: 1;
}

.section > .name > .toggle {
text-align: right;
background: none;
border: none;
font-size: 13px;
}

.section .section {
margin-left: 16px;
margin-bottom: 0px;
Expand Down
1 change: 1 addition & 0 deletions templates/simple/src/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Page = {
export type Section = Omit<Page, "hide"> & {
hide?: boolean;
virtual?: boolean;
expanded?: boolean;
pages: Page[]; // md-files
sections: Section[]; // folders
};
Expand Down
1 change: 1 addition & 0 deletions website/book/configuration/section.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ To summarize, here are the properties you can set in the frontmatter:
| :------- | :--------------------------------------------------------------------------------------------------------------------- |
| ... | Every Property from a [page](/configuration/page) |
| virtual | Everything will appear on the same level as the parent. Be aware that the index property does not work across folders. |
| expanded | Defines whether the section is expanded on page load. |

If the `index.md` file only contains a front matter and is otherwise
empty, it will appear in the navigation as a non-clickable item.