-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web): Introduce
Toast
JavaScript plugin #DS-1115
- Loading branch information
1 parent
2e394dc
commit b44a384
Showing
10 changed files
with
271 additions
and
34 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
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,103 @@ | ||
import BaseComponent from './BaseComponent'; | ||
import { | ||
ATTRIBUTE_ARIA_EXPANDED, | ||
ATTRIBUTE_DATA_TARGET, | ||
CLASS_NAME_HIDDEN, | ||
CLASS_NAME_OPEN, | ||
CLASS_NAME_TRANSITIONING, | ||
} from './constants'; | ||
import { enableDismissTrigger, enableToggleTrigger, executeAfterTransition, SpiritConfig } from './utils'; | ||
import { EventHandler, SelectorEngine } from './dom'; | ||
|
||
const NAME = 'toast'; | ||
const DATA_KEY = `${NAME}`; | ||
const EVENT_KEY = `.${DATA_KEY}`; | ||
|
||
const EVENT_HIDE = `hide${EVENT_KEY}`; | ||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`; | ||
const EVENT_SHOW = `show${EVENT_KEY}`; | ||
const EVENT_SHOWN = `shown${EVENT_KEY}`; | ||
|
||
class Toast extends BaseComponent { | ||
isShown: boolean; | ||
triggers: HTMLElement[]; | ||
|
||
static get NAME() { | ||
return NAME; | ||
} | ||
|
||
constructor(element: SpiritElement, config?: SpiritConfig) { | ||
super(element, config); | ||
|
||
this.triggers = this.getTriggers(); | ||
|
||
this.isShown = this.checkShownState(); | ||
} | ||
|
||
checkShownState() { | ||
return this.element.classList.contains(CLASS_NAME_OPEN) || !this.element.classList.contains(CLASS_NAME_HIDDEN); | ||
} | ||
|
||
getTriggers() { | ||
const id = this.element && (this.element.getAttribute('id') as string); | ||
|
||
return SelectorEngine.findAll(`[${ATTRIBUTE_DATA_TARGET}="#${id}"]`); | ||
} | ||
|
||
show() { | ||
if (this.isShown) { | ||
return; | ||
} | ||
|
||
const showEvent = EventHandler.trigger(this.element, Toast.eventName(EVENT_SHOW)) as Event; | ||
if (showEvent.defaultPrevented) { | ||
return; | ||
} | ||
|
||
this.triggers.forEach((element) => { | ||
element?.setAttribute(ATTRIBUTE_ARIA_EXPANDED, 'true'); | ||
}); | ||
|
||
this.element.classList.remove(CLASS_NAME_HIDDEN); | ||
this.element.classList.add(CLASS_NAME_OPEN); | ||
this.element.classList.add(CLASS_NAME_TRANSITIONING); | ||
|
||
executeAfterTransition(this.element, () => { | ||
EventHandler.trigger(this.element, Toast.eventName(EVENT_SHOWN)); | ||
this.element.classList.remove(CLASS_NAME_TRANSITIONING); | ||
}); | ||
|
||
this.isShown = true; | ||
} | ||
|
||
hide() { | ||
if (!this.isShown) { | ||
return; | ||
} | ||
|
||
const hideEvent = EventHandler.trigger(this.element, Toast.eventName(EVENT_HIDE)) as Event; | ||
if (hideEvent.defaultPrevented) { | ||
return; | ||
} | ||
|
||
this.triggers.forEach((element) => { | ||
element?.setAttribute(ATTRIBUTE_ARIA_EXPANDED, 'false'); | ||
}); | ||
|
||
this.element.classList.remove(CLASS_NAME_OPEN); | ||
this.element.classList.add(CLASS_NAME_HIDDEN); | ||
this.element.classList.add(CLASS_NAME_TRANSITIONING); | ||
|
||
executeAfterTransition(this.element, () => { | ||
EventHandler.trigger(this.element, Toast.eventName(EVENT_HIDDEN)); | ||
this.element.remove(); | ||
}); | ||
|
||
this.isShown = false; | ||
} | ||
} | ||
|
||
enableToggleTrigger(Toast, 'show', 'target'); | ||
enableDismissTrigger(Toast, 'hide', 'target'); | ||
|
||
export default Toast; |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
export const ARIA_EXPANDED_ATTRIBUTE = 'aria-expanded'; | ||
export const ARIA_CONTROLS_ATTRIBUTE = 'aria-controls'; | ||
export const ATTRIBUTE_ARIA_EXPANDED = 'aria-expanded'; | ||
export const ATTRIBUTE_ARIA_CONTROLS = 'aria-controls'; | ||
export const ATTRIBUTE_DATA_DISMISS = 'data-spirit-dismiss'; | ||
export const ATTRIBUTE_DATA_TARGET = 'data-spirit-target'; | ||
export const ATTRIBUTE_DATA_TOGGLE = 'data-spirit-toggle'; | ||
|
||
export const NAME_DATA_TOGGLE = 'data-spirit-toggle'; | ||
export const NAME_DATA_TARGET = 'data-spirit-target'; | ||
|
||
export const CLASSNAME_EXPANDED = 'is-expanded'; | ||
export const CLASSNAME_OPEN = 'is-open'; | ||
export const CLASSNAME_TRANSITION = 'is-transitioning'; | ||
export const CLASS_NAME_HIDDEN = 'is-hidden'; | ||
export const CLASS_NAME_OPEN = 'is-open'; | ||
export const CLASS_NAME_TRANSITIONING = 'is-transitioning'; | ||
export const CLASS_NAME_VISIBLE = 'is-visible'; |
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
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
Oops, something went wrong.