Skip to content

feat(): Introduce accordion component #106

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

Merged
merged 4 commits into from
Oct 10, 2019
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
23 changes: 23 additions & 0 deletions js/accordion-group/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const COMPONENT_NAME = 'data-accordion-group'
const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`
const ACCORDION_SELECTOR = `[data-accordion]`

export default function () {
const components = document.querySelectorAll(COMPONENT_SELECTOR)

components.forEach(component => {
const accordions = component.querySelectorAll(ACCORDION_SELECTOR)

accordions.forEach(currentAccordion => {
currentAccordion.addEventListener('accordion.triggered', () => {
accordions.forEach(accordion => {
let activeClass = accordion.getAttribute('data-class-active') || 'active'

if (accordion.classList.contains(activeClass) && accordion != currentAccordion) {
accordion.classList.remove(activeClass)
}
})
})
})
})
}
29 changes: 29 additions & 0 deletions js/accordion/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Creates a custom DOM event.
*
* @param {String} name
* @return {Event}
*/
function createEvent (name) {
const event = document.createEvent('Event')

event.initEvent(name, true, true)

return event
}

/**
* First visit
*
* Example:
* el.addEventListener('cookies.firstVisit', () => { ... })
*
* @type {Event}
*/
const accordionTriggered = createEvent('accordion.triggered')



export {
accordionTriggered
}
3 changes: 3 additions & 0 deletions js/accordion/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { accordionTriggered } from './events'

const COMPONENT_NAME = 'data-accordion'
const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`
const TRIGGER_SELECTOR = `[data-ref="accordion[trigger]"]`
Expand All @@ -17,6 +19,7 @@ export default function () {
trigger.addEventListener('click', (event) => {
event.preventDefault()

components[i].dispatchEvent(accordionTriggered)
components[i].classList.toggle(activeClass)
})
}
Expand Down
2 changes: 2 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import feedback from './feedback'
import accordion from './accordion'
import accordionGroup from './accordion-group'
import searchNavbar from './search-navbar'
import menuMobile from './menu-mobile'
import headingsAnchors from './headings-anchors'
import dropdownMenu from './dropdown-menu'

feedback()
accordion()
accordionGroup()
searchNavbar()
menuMobile()
headingsAnchors()
Expand Down
20 changes: 20 additions & 0 deletions src/_includes/components/faq.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="faq">
{% for item in include.items %}
<div class="accordion" data-accordion-group>

<h3>{{ item[0] }}</h3>

{% for itemEl in item[1] %}
<div class="accordion__item" data-accordion data-class-active="accordion--active">
<div class="accordion__heading" data-ref="accordion[trigger]">
{{ itemEl.title }}
<div class="accordion__icon">
{% include icons/symbols/chevron-down.svg %}
</div>
</div>
<div class="accordion__body" data-ref="accordion[body]">{{ itemEl.content }}</div>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
50 changes: 50 additions & 0 deletions src/_sass/components/_accordion.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.accordion {
$this: &;

&__item {
margin-top: 25px;

& + & {
border-top: 1px solid rgba(color(secondary), 0.16);
margin-top: 0;
}
}

&__heading {
font-size: 15px;
font-weight: 500;
color: color(secondary);
line-height: 1.71;
position: relative;
margin: 17px 0;
cursor: pointer;
}

&__body {
display: none;
padding-bottom: 20px;
}

&__icon {
width: 8px;
color: rgba(color(secondary-dark), 0.5);
position: absolute;
top: 50%;
right: 20px;
transform: translateY(-50%);
}

&--active {
#{$this}__icon {
transform: translateY(-50%) rotate(180deg);
}

#{$this}__heading {
margin-bottom: 12px;
}

#{$this}__body {
display: block;
}
}
}
5 changes: 5 additions & 0 deletions src/_sass/components/_faq.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.faq {
> * + * {
margin-top: 50px;
}
}
2 changes: 2 additions & 0 deletions src/_sass/segment.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
@import "components/feedback-box";
@import "components/media-icon";
@import "components/media-thumbnail";
@import "components/accordion";
@import "components/faq";
@import "components/avatar";

// Pages
Expand Down
17 changes: 17 additions & 0 deletions src/styleguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ title: Styleguide
description: The styleguide of front-end components
hidden: true
layout: page
faq:
Source:
- title : "Should I track client or server-side?"
content : "One of the most common questions we receive is: “Should I use one of your client-side libraries or one of your server-side libraries?” This is such an important topic that we’ve written up an in-depth article in our Analytics Academy: When to Track on the Client vs Server. It’s worth a read!"
- title : "What are best practices in identifying users?"
content : "One of the most common questions we receive is: “Should I use one of your client-side libraries or one of your server-side libraries?” This is such an important topic that we’ve written up an in-depth article in our Analytics Academy: When to Track on the Client vs Server. It’s worth a read!"
Cloud Sources:
- title : "What are cloud sources?"
content : "One of the most common questions we receive is: “Should I use one of your client-side libraries or one of your server-side libraries?” This is such an important topic that we’ve written up an in-depth article in our Analytics Academy: When to Track on the Client vs Server. It’s worth a read!"
- title : "How do cloud sources work?"
content : "One of the most common questions we receive is: “Should I use one of your client-side libraries or one of your server-side libraries?” This is such an important topic that we’ve written up an in-depth article in our Analytics Academy: When to Track on the Client vs Server. It’s worth a read!"
contributors:
- name: Paul Mccall
position: Lead Developer
Expand Down Expand Up @@ -124,3 +135,9 @@ analytics.identify('025pikachu025', {
## Callout

{% include components/callout.html title="Get started with Segment" content="Segment is the easiest way to integrate your websites & mobile apps data to 250+ analytics and growth tools." %}

---

## FAQ

{% include components/faq.html items=page.faq %}