Skip to content

Commit

Permalink
Accordion (#120)
Browse files Browse the repository at this point in the history
* First draft accordion component

* Further changes to the new accordion

* Fix incorrectly nested CSS

* Update CHANGELOG.md

* Export Accordion from all.mjs

* Remove double marker on accordions in Safari

* Lint

* Add TODO

* Update Jest ignore paths

* Add details component

* Small CSS tweaks
  • Loading branch information
ahosgood authored May 20, 2024
1 parent b4b5e50 commit 3b8255c
Show file tree
Hide file tree
Showing 32 changed files with 1,028 additions and 66 deletions.
4 changes: 4 additions & 0 deletions .github/actions/prototype-kit-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ runs:
shell: bash
- name: Add imports to template
run: >
echo -e "{% from \"nationalarchives/components/accordion/macro.njk\" import tnaAccordion %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/breadcrumbs/macro.njk\" import tnaBreadcrumbs %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/button/macro.njk\" import tnaButton %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/card/macro.njk\" import tnaCard %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
Expand All @@ -43,6 +44,7 @@ runs:
echo -e "{% from \"nationalarchives/components/cookie-banner/macro.njk\" import tnaCookieBanner %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/date-input/macro.njk\" import tnaDateInput %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/date-search/macro.njk\" import tnaDateSearch %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/details/macro.njk\" import tnaDetails %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/error-summary/macro.njk\" import tnaErrorSummary %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/featured-records/macro.njk\" import tnaFeaturedRecords %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
echo -e "{% from \"nationalarchives/components/footer/macro.njk\" import tnaFooter %}\n$(cat prototype/app/views/index.html)" > prototype/app/views/index.html &&
Expand Down Expand Up @@ -70,6 +72,7 @@ runs:
- name: Add components to template
run: >
echo "\n{% block bodyEnd %}" >> prototype/app/views/index.html &&
echo "{{ tnaAccordion({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaBreadcrumbs({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaButton({text:\"I am a button\",url:\"#\"}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaCard({}) }}" >> prototype/app/views/index.html &&
Expand All @@ -78,6 +81,7 @@ runs:
echo "{{ tnaCookieBanner({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaDateInput({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaDateSearch({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaDetails({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaErrorSummary({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaFeaturedRecords({}) }}" >> prototype/app/views/index.html &&
echo "{{ tnaFooter({}) }}" >> prototype/app/views/index.html &&
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/nationalarchives/tna-frontend/compare/v0.1.54...HEAD)

### Added

- New accordion and details components added

### Changed
### Deprecated
### Removed
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
modulePathIgnorePatterns: ["/package", "/storybook"],
modulePathIgnorePatterns: ["/package/", "/storybook/"],
testEnvironment: "jsdom",
transform: {
"\\.m?js$": "babel-jest",
Expand Down
7 changes: 7 additions & 0 deletions src/nationalarchives/all.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Accordion } from "./components/accordion/accordion.mjs";
import { Breadcrumbs } from "./components/breadcrumbs/breadcrumbs.mjs";
import { CookieBanner } from "./components/cookie-banner/cookie-banner.mjs";
import { ErrorSummary } from "./components/error-summary/error-summary.mjs";
Expand Down Expand Up @@ -41,6 +42,11 @@ const initAll = (options) => {
const $scope =
options.scope instanceof HTMLElement ? options.scope : document;

const $accordions = $scope.querySelectorAll('[data-module="tna-accordion"]');
$accordions.forEach(($accordion) => {
new Accordion($accordion);
});

const $breadcrumbs = $scope.querySelector('[data-module="tna-breadcrumbs"]');
if ($breadcrumbs) {
new Breadcrumbs($breadcrumbs);
Expand Down Expand Up @@ -115,6 +121,7 @@ const initAll = (options) => {
export {
initAll,
Cookies,
Accordion,
Breadcrumbs,
CookieBanner,
ErrorSummary,
Expand Down
2 changes: 2 additions & 0 deletions src/nationalarchives/components/_index.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use "accordion";
@use "breadcrumbs";
@use "button";
@use "card";
Expand All @@ -6,6 +7,7 @@
@use "cookie-banner";
@use "date-input";
@use "date-search";
@use "details";
@use "error-summary";
@use "featured-records";
@use "footer";
Expand Down
1 change: 1 addition & 0 deletions src/nationalarchives/components/accordion/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@use "accordion";
35 changes: 35 additions & 0 deletions src/nationalarchives/components/accordion/accordion.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export class Accordion {
polyfillRequired = null;

constructor($module) {
this.$module = $module;
this.$firstDetailsElement =
$module && $module.querySelector(".tna-accordion__details[name]");
this.$detailsElements =
$module &&
this.$firstDetailsElement &&
document.querySelectorAll(
`.tna-accordion__details[name="${this.$firstDetailsElement.getAttribute("name")}"]`,
);
if (!this.$module || !this.$detailsElements) {
return;
}

Array.from(this.$detailsElements).forEach(($detailsElement) => {
$detailsElement.addEventListener("toggle", (e) => {
const $thisDetailsElement = e.target;
if ($thisDetailsElement.hasAttribute("open")) {
Array.from(this.$detailsElements)
.filter(
($eachDetailsElement) =>
$eachDetailsElement !== $thisDetailsElement &&
$eachDetailsElement.hasAttribute("open"),
)
.forEach(($eachDetailsElement) =>
$eachDetailsElement.removeAttribute("open"),
);
}
});
});
}
}
116 changes: 116 additions & 0 deletions src/nationalarchives/components/accordion/accordion.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
@use "sass:math";
@use "../../tools/colour";
@use "../../tools/spacing";
@use "../../tools/typography";

.tna-accordion {
@include spacing.space-above;

@include colour.colour-border("keyline", 1px, solid, top);
// @include colour.thick-keyline-dark(top);
// @include colour.thick-keyline-accent(top);

&__details {
@include colour.colour-border("keyline", 1px, solid, bottom);

position: relative;
z-index: 1;
}

&__details:has(&__summary:focus) {
z-index: 2;
}

&__summary {
padding: spacing.space(0.5) spacing.space(2.5) spacing.space(0.5)
spacing.space(1);

position: relative;

@include typography.main-font-weight-bold;
list-style: none;

border-radius: 0.1px;

cursor: pointer;

&::-webkit-details-marker {
display: none;
}

&::before {
content: "";

width: 0;
height: 0;

position: absolute;
top: calc(50% - 0.5rem);
right: 0.75rem;

border-width: #{math.div(math.sqrt(3), 2)}rem 0.5rem 0 0.5rem;
border-color: colour.colour-var("font-light") transparent transparent
transparent;
border-style: solid;
}

&:hover {
@include typography.interacted-text-decoration;
@include colour.colour-background("background-tint");

&::before {
border-top-color: colour.colour-var("font-dark");
}
}
}

&__content {
padding: spacing.space(1);

&:has(.tna-table-wrapper):has(.tna-table) {
padding-right: 0;
padding-left: 0;

.tna-table {
&__caption {
padding-top: 0;
padding-bottom: spacing.space(1);

font-size: inherit;
line-height: inherit;
text-align: left;
caption-side: top;
}
}

.tna-table-wrapper {
padding-right: 0;
padding-left: 0;

left: 0;

.tna-table {
width: calc(100% - #{spacing.space(2)});
margin-right: spacing.space(1);
margin-left: spacing.space(1);
}
}
}
}

&__details[open] &__summary {
&::before {
top: calc(50% - #{math.div(math.sqrt(3), 4)}rem);

border-width: 0 0.5rem #{math.div(math.sqrt(3), 2)}rem 0.5rem;
border-color: transparent transparent colour.colour-var("font-light")
transparent;
}

&:hover {
&::before {
border-bottom-color: colour.colour-var("font-dark");
}
}
}
}
Loading

0 comments on commit 3b8255c

Please sign in to comment.