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

fix: update accordion content and css #33

Merged
merged 3 commits into from
May 30, 2023
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
24 changes: 18 additions & 6 deletions packages/vanilla/src/components/accordion/accordion.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class Accordion {
constructor(domNode) {
this.accordionNode = domNode;
constructor(accordion) {
this.accordionNode = accordion;
this.accordionControl = this.accordionNode.querySelector('.cbp-accordion__control');
this.accordionContent = this.accordionNode.querySelector('.cbp-accordion__content');

this.accordionNode.addEventListener('click', this.toggle);
this.accordionControl.setAttribute('aria-expanded', false)

this.accordionNode.addEventListener('click', () => {
this.toggle()
});
}

toggle(event) {
const wrapper = event.target.closest('.cbp-accordion__item');
wrapper.classList.toggle('active');
toggle() {
const previousState =
this.accordionControl.hasAttribute('aria-expanded') &&
this.accordionControl.getAttribute('aria-expanded') === 'true'
? true
: false;

this.accordionControl.setAttribute('aria-expanded', !previousState);
this.accordionContent.toggleAttribute('hidden');
}
}

Expand Down
94 changes: 57 additions & 37 deletions packages/vanilla/src/components/accordion/accordion.stories.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
export default {
title: 'Patterns/Accordion',
argTypes: {
label: { control: 'text' },
onClick: { action: 'onClick' },
},
defaultAccordion: {
name: 'Accordion (Default)',
},
badgeAccordion: {
name: 'Accordion w/ Badge',
},
iconAccordion: {
name: 'Accordion w/ Icon',
}
}
};



const Template = ({ label, ...args }) => {
const Template = ({ defaultAccordion, badgeAccordion, iconAccordion }) => {
return `
<div class="cbp-accordion">
<div class="cbp-accordion__item">
<button class="cbp-accordion__title" aria-expanded="false" aria-controls="accordion-demo-1">
<div>
<i class="fas fa-angle-down"></i>
<span>Accordion Control</span>
</div>
<div class="cbp-accordion__item ${defaultAccordion.danger ? 'cbp-accordion__item--danger' : ''}">
<button class="cbp-accordion__control" id="accordion1" aria-controls="accordion-content-1">
<span class="cbp-accordion__title">
<i class="fas fa-chevron-down"></i>
${defaultAccordion.title}
</span>
</button>

<div class="cbp-accordion__content" aria-labelledby="accordion-demo-1">
<h6 class="cbp-text-heading-3xs text-italic">Content Heading</h6>
<hr>
<p>Accordion content here.</p>
<div class="cbp-accordion__footer">
<p>Accordion footer</p>
</div>
<div class="cbp-accordion__content" id="accordion-content-1" aria-labelledby="accordion1" hidden>
<p class="cbp-font-style-italic">Content Area</p>
</div>
</div>

<div class="cbp-accordion__item">
<button class="cbp-accordion__title" aria-expanded="false" aria-controls="accordion-demo-1">
<div>
<i class="fas fa-angle-down"></i>
<span>Accordion Control with badge</span>
</div>
<div class="cbp-badge">
<span class="cbp-text-badge">7</span>
</div>
<div class="cbp-accordion__item ${badgeAccordion.danger ? 'cbp-accordion__item--danger' : ''}">
<button class="cbp-accordion__control" id="accordion2" aria-controls="accordion-content-2">
<span class="cbp-accordion__title">
<i class="fas fa-chevron-down"></i>
${badgeAccordion.title}
</span>
<div class="cbp-badge">
<span class="cbp-text-badge">16</span>
</div>
</button>

<div class="cbp-accordion__content" aria-labelledby="accordion-demo-1">
<h6 class="cbp-text-heading-3xs text-italic">Content Heading</h6>
<hr>
<p>Accordion content here.</p>
<div class="cbp-accordion__footer">
<p>Accordion footer</p>
</div>
<div class="cbp-accordion__content" id="accordion-content-2" aria-labelledby="accordion2" hidden>
<p class="cbp-font-style-italic">Content Area</p>
</div>
</div>

<div class="cbp-accordion__item ${iconAccordion.danger ? 'cbp-accordion__item--danger' : ''}">
<button class="cbp-accordion__control" id="accordion3" aria-controls="accordion-content-3">
<span class="cbp-accordion__title">
<i class="fas fa-chevron-down"></i>
${iconAccordion.title}
</span>
<i class="fas fa-exclamation-triangle"></i>
</button>
<div class="cbp-accordion__content" id="accordion-content-3" aria-labelledby="accordion3" hidden>
<p class="cbp-font-style-italic">Content Area</p>
</div>
</div>
</div>
`;
};

export const Accordion = Template.bind({});
Accordion.args = {};
Accordion.args = {
defaultAccordion: {
title: 'Accordion (Default)',
danger: false
},
badgeAccordion: {
title: 'Accordion w/ Badge',
danger: false
},
iconAccordion: {
title: 'Accordion w/ Icon',
danger: false
}
};
2 changes: 1 addition & 1 deletion packages/vanilla/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
/**
* Accordion Component
*/
SelectorEngine.findAll(".cbp-accordion__title").forEach((accordion) => {
SelectorEngine.findAll(".cbp-accordion__item").forEach((accordion) => {
addOrInstantiate(Accordion, accordion);
});

Expand Down
199 changes: 89 additions & 110 deletions packages/vanilla/src/sass/components/accordion/_index.scss
Original file line number Diff line number Diff line change
@@ -1,148 +1,127 @@
//@use '../../base/colors' as *;
@use '../../typography' as *;
@use './states' as *;
//@use '../../tokens/font' as *;

.cbp-accordion {
margin: 24px 0;
margin-top: var(--cbp-space-6x);
dannyk3941 marked this conversation as resolved.
Show resolved Hide resolved
margin-bottom: var(--cbp-space-6x);
width: 100%;
}

.cbp-accordion__item {
margin-bottom: 12px;

&:last-of-type {
margin-bottom: 0;
}
}

.cbp-accordion__title {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
color: var(--cbp-color-text-darkest);
background: var(--cbp-color-interactive-neutral-lighter);
border: none;
outline: transparent;
padding: 16px;
transition: all 300ms;

div:first-child i {
color: var(--cbp-color-text-darker);
font-size: var(--cbp-font-size-heading-md);
transition: transform .2s ease-in-out;
}
.cbp-accordion__item {
margin-bottom: var(--cbp-space-3x);
dannyk3941 marked this conversation as resolved.
Show resolved Hide resolved
}

div:first-child span {
@extend .cbp-heading-s;
margin-left: 16px;
}
.cbp-accordion__control {
display: flex;
align-items: center;
background: var(--cbp-color-interactive-neutral-lighter);
border: none;
color: var(--cbp-color-text-darkest);
justify-content: space-between;
outline: 0;
min-height: var(--cbp-space-13x);
padding-left: var(--cbp-space-4x);
padding-right: var(--cbp-space-4x);
transition: all 300ms;
width: 100%;

&:hover {
background-color: var(--cbp-color-interactive-neutral-light);
}
& i {
font-size: var(--cbp-font-size-heading-md);

&:focus {
@include accordion-focus;
@include accordion-text-light;
@include accordion-badge(var(--cbp-color-white), var(--cbp-color-base-secondary-base));
&:first-child {
margin-right: var(--cbp-space-2x);
transition: transform var(--cbp-motion-duration-shortest) ease-in-out;
}
}

/* TODO: Leverage the cascade. Put states last. */
&:focus + .cbp-accordion__content {
@include accordion-content-border(var(--cpb-color-interactive-focus-dark), true);
}
&:hover {
background-color: var(--cbp-color-interactive-neutral-light);
}

.cbp-accordion__content {
background: var(--cbp-color-white);
display: none;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-in-out;

/* TODO: wouldn't it be easier to apply padding to the content panel
rather than margin to every child inside of it, possibly overriding something?
*/
> * {
margin: 16px
&:focus {
background: var(--cbp-color-interactive-focus-dark);
color: var(--cbp-color-text-lightest);
outline-width: var(--cbp-border-size-md);
outline-style: solid;
outline-color: var(--cbp-color-white);
outline-offset: calc(-1 * var(--cbp-space-2x));

& > .cbp-badge {
background-color: var(--cbp-color-white);
color: var(--cbp-color-base-secondary-base);
}
}
}

.cbp-accordion__control[aria-expanded="true"] {
background-color: var(--cbp-color-base-secondary-base);
color: var(--cbp-color-text-lightest);

.cbp-accordion__title--danger {
background: var(--cbp-color-danger-base);
& i:first-child {
transform: rotate(-180deg);
}

@include accordion-text-light;
@include accordion-badge(var(--cbp-color-white), var(--cbp-color-danger-base));
& .cbp-badge {
background-color: var(--cbp-color-white);
color: var(--cbp-color-base-secondary-base);
}

&:hover {
background: var(--cbp-color-danger-dark);
background-color: var(--cbp-color-base-secondary-dark);

& + .cbp-accordion__content {
border-color: var(--cbp-color-base-secondary-dark);
}
}

&:focus {
@include accordion-focus;
@include accordion-badge(var(--cbp-color-white), var(--cbp-color-danger-base));
background-color: var(--cbp-color-interactive-focus-dark);

& + .cbp-accordion__content {
border-color: var(--cbp-color-interactive-focus-dark);
}
}
}

.active {
.cbp-accordion__title {
background-color: var(--cbp-color-base-secondary-base);

@include accordion-text-light;
.cbp-accordion__title {
align-items: center;
display: flex;
font-weight: var(--cbp-font-weight-medium);
font-size: var(--cbp-font-size-heading-sm);
}

div:first-child i {
transform: rotate(-180deg);
}
.cbp-accordion__content {
background: var(--cbp-color-white);
border: var(--cbp-border-size-xl) solid var(--cbp-color-base-secondary-base);
border-top: 0;
padding: var(--cbp-space-4x);
}

@include accordion-badge(var(--cbp-color-white), var(--cbp-color-base-secondary-base));

.cbp-accordion__title--danger {
background: var(--cbp-color-danger-base);
color: var(--cbp-color-text-lightest);

.cbp-badge span {
color: var(--cbp-color-danger-base);
}
}
.cbp-accordion__item--danger > .cbp-accordion__control {
background: var(--cbp-color-danger-base);
color: var(--cbp-color-text-lightest);

.cbp-accordion__title--danger + .cbp-accordion__content {
border-color: var(--cbp-color-danger-base);
}
& + .cbp-accordion__content {
border-color: var(--cbp-color-danger-base);
}

.cbp-accordion__content {
display: block;
max-height: fit-content;
@include accordion-content-border(var(--cbp-color-base-secondary-base));

.cbp-accordion__footer {
align-items: center;
display: flex;
justify-content: space-between;
margin-bottom: 16px;
margin-top: 16px;
}

& .cbp-badge {
background-color: var(--cbp-color-white);
color: var(--cbp-color-danger-base);
}
}

// Hover States
.cbp-accordion__item.active:hover {
.cbp-accordion__title {
background-color: var(--cbp-color-base-secondary-dark);
}
&:hover {
background: var(--cbp-color-danger-dark);

.cbp-accordion__content {
@include accordion-content-border(var(--cbp-color-base-secondary-base));
& + .cbp-accordion__content {
border-color: var(--cbp-color-danger-dark);
}
}

.cbp-accordion__title--danger {
background-color: var(--cbp-color-danger-dark);
}
&:focus {
background: var(--cbp-color-interactive-focus-dark);

.cbp-accordion__title--danger + .cbp-accordion__content {
@include accordion-content-border(var(--cbp-color-danger-dark));
& + .cbp-accordion__content {
border-color: var(--cbp-color-interactive-focus-dark);
}
}
}
Loading