-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
251 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ComponentManager'; | ||
export * from './ComponentManager'; | ||
export * from './ClickTriggerComponent'; |
File renamed without changes.
88 changes: 88 additions & 0 deletions
88
packages/vanilla/src/components/Collapse/Collapse.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { CollapseManager } from './CollapseManager'; | ||
|
||
interface CollapseProps { | ||
id: string; | ||
triggerContent: string; | ||
content: string; | ||
horizontal?: boolean; | ||
} | ||
|
||
export default { | ||
title: 'Vanilla JavaScript/Collapse', | ||
render: ({ | ||
id, | ||
triggerContent, | ||
content, | ||
horizontal = false, | ||
}: CollapseProps) => { | ||
const container = document.createElement('div'); | ||
container.style.display = 'flex'; | ||
container.style.alignItems = 'stretch'; | ||
|
||
if (!horizontal) { | ||
container.style.flexDirection = 'column'; | ||
container.style.width = '300px'; | ||
} else { | ||
container.style.height = '200px'; | ||
} | ||
|
||
const triggerDiv = document.createElement('div'); | ||
triggerDiv.innerHTML = triggerContent; | ||
triggerDiv.setAttribute('data-toggle', 'collapse') | ||
triggerDiv.setAttribute('data-target', `#${id}`); | ||
triggerDiv.setAttribute('aria-expanded', 'true'); | ||
triggerDiv.style.background = 'var(--color-primary-500)' | ||
if (horizontal) { | ||
triggerDiv.style.textOrientation = 'mixed' | ||
triggerDiv.style.writingMode = 'vertical-rl'; | ||
} | ||
|
||
container.appendChild(triggerDiv); | ||
|
||
const contentDiv = document.createElement('div'); | ||
contentDiv.className = 'collapse show'; | ||
if (horizontal) { | ||
contentDiv.classList.add('horizontal'); | ||
|
||
const childDiv = document.createElement('div'); | ||
childDiv.style.width = '300px'; | ||
childDiv.innerHTML = content; | ||
contentDiv.append(childDiv); | ||
} else { | ||
contentDiv.innerHTML = content; | ||
} | ||
|
||
contentDiv.id = id; | ||
contentDiv.style.background = 'var(--color-primary-100)' | ||
container.appendChild(contentDiv); | ||
|
||
const collapseManager = new CollapseManager(); | ||
container.addEventListener('click', (ev) => { | ||
collapseManager.getInstance(ev.target as HTMLElement)?.executeByClick(); | ||
}); | ||
|
||
return container; | ||
}, | ||
argTypes: { | ||
id: { control: 'text', }, | ||
triggerContent: { control: 'text' }, | ||
content: { control: 'text', }, | ||
}, | ||
}; | ||
|
||
export const Default = { | ||
args: { | ||
id: 'example-default-collapse', | ||
triggerContent: 'Click here to toggle', | ||
content: 'John doe<br/>John doe<br/>John doe<br/>John doe<br/>John doe<br/>John doe<br/>' | ||
} | ||
}; | ||
|
||
export const Horizontal = { | ||
args: { | ||
id: 'example-horizontal-collapse', | ||
triggerContent: 'Click here to toggle', | ||
content: 'John doe<br/>John doe<br/>John doe<br/>John doe<br/>John doe<br/>John doe<br/>', | ||
horizontal: true, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { ClickTriggerComponent } from '../../base'; | ||
import { capFirst } from '../../utils/text'; | ||
import { executeAfterTransition } from '../../utils/transitions'; | ||
import { CollapseElement } from './CollapseElement'; | ||
|
||
export interface CollapseContentSize { | ||
property: 'width' | 'height'; | ||
value: string; | ||
} | ||
|
||
export const CLASS_NAME_SHOW = 'show'; | ||
export const CLASS_NAME_COLLAPSE = 'collapse'; | ||
export const CLASS_NAME_COLLAPSING = 'collapsing'; | ||
export const CLASS_NAME_COLLAPSED = 'collapsed'; | ||
export const CLASS_NAME_HORIZONTAL = 'horizontal'; | ||
|
||
export const DIMENSION_VERTICAL = 'VERTICAL' | ||
export const DIMENSION_HORIZONTAL = 'HORIZONTAL' | ||
export type DIMENSION_TYPE = 'VERTICAL' | 'HORIZONTAL'; | ||
export const DIMENSIONS: Record<DIMENSION_TYPE, 'width' | 'height'> = { | ||
'VERTICAL': 'height', | ||
'HORIZONTAL': 'width', | ||
} | ||
|
||
export class Collapse implements ClickTriggerComponent { | ||
element: CollapseElement; | ||
|
||
constructor(element: CollapseElement) { | ||
this.element = element; | ||
} | ||
|
||
getContentSize(content: HTMLElement, isOpen: boolean): CollapseContentSize { | ||
const dimension: DIMENSION_TYPE = content.classList.contains(CLASS_NAME_HORIZONTAL) | ||
? DIMENSION_HORIZONTAL | ||
: DIMENSION_VERTICAL; | ||
const property = `scroll${capFirst(DIMENSIONS[dimension])}`; | ||
if (!isOpen) { | ||
return { | ||
value: '', | ||
property: DIMENSIONS[dimension], | ||
}; | ||
} | ||
return { | ||
value: `${(content as any)[property]}px`, | ||
property: DIMENSIONS[dimension], | ||
}; | ||
} | ||
|
||
toggle() { | ||
const { toggle, content } = this.element; | ||
|
||
if (content.classList.contains(CLASS_NAME_COLLAPSING)) { | ||
return; | ||
} | ||
|
||
const isOpenAttr = toggle.getAttribute('aria-expanded'); | ||
let isOpen = true; | ||
if (isOpenAttr) { | ||
isOpen = Boolean(JSON.parse(isOpenAttr)); | ||
} | ||
|
||
const { value, property } = this.getContentSize(content, isOpen); | ||
|
||
content.style[property] = value; | ||
content.offsetHeight; // reset animation | ||
|
||
content.classList.add(CLASS_NAME_COLLAPSING); | ||
content.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW) | ||
content.classList.toggle(CLASS_NAME_COLLAPSED, isOpen); | ||
toggle.setAttribute('aria-expanded', JSON.stringify(!isOpen)); | ||
|
||
const complete = () => { | ||
content.classList.remove(CLASS_NAME_COLLAPSING); | ||
content.classList.add(CLASS_NAME_COLLAPSE); | ||
if (!isOpen) { | ||
content.classList.add(CLASS_NAME_SHOW); | ||
} | ||
}; | ||
executeAfterTransition(complete, content); | ||
|
||
const { value: destValue } = this.getContentSize(content, !isOpen); | ||
|
||
content.style[property] = destValue; | ||
} | ||
|
||
executeByClick() { | ||
this.toggle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface CollapseElement { | ||
id: string; | ||
toggle: HTMLElement; | ||
content: HTMLElement; | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/vanilla/src/components/Collapse/CollapseManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ComponentManager, ClickTriggerComponentManager } from '../../base'; | ||
import { CollapseElement } from './CollapseElement'; | ||
import { Collapse } from './Collapse'; | ||
|
||
const TRIGGER_SELECTOR = '[data-toggle="collapse"]'; | ||
|
||
export class CollapseManager extends ComponentManager<CollapseElement, Collapse> { | ||
protected getElement(target?: HTMLElement | null): CollapseElement | null { | ||
if (!target) { | ||
return null; | ||
} | ||
const toggleClosest = target.closest<HTMLElement>(TRIGGER_SELECTOR); | ||
if (!toggleClosest) { | ||
return null; | ||
} | ||
const contentQuery = toggleClosest.getAttribute('data-target'); | ||
if (!contentQuery) { | ||
return null; | ||
} | ||
const contentEl = document.querySelector<HTMLElement>(contentQuery); | ||
if (!contentEl) { | ||
return null; | ||
} | ||
return { | ||
id: contentEl.id, | ||
toggle: toggleClosest, | ||
content: contentEl, | ||
}; | ||
} | ||
|
||
protected createInstance(element: CollapseElement): Collapse { | ||
return new Collapse(element); | ||
} | ||
} | ||
|
||
ClickTriggerComponentManager.register(new CollapseManager()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './CollapseElement'; | ||
export * from './CollapseManager'; | ||
export * from './Collapse'; |
89 changes: 0 additions & 89 deletions
89
packages/vanilla/src/components/collapse/collapse.browser.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.