-
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 19f440b
Showing
9 changed files
with
181 additions
and
32 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,85 @@ | ||
import BaseComponent from './BaseComponent'; | ||
import { ATTRIBUTE_ARIA_EXPANDED, 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 = 'tooltip'; | ||
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}`; | ||
|
||
const SELECTOR_DATA_DISMISS = '[data-spirit-dismiss]'; | ||
|
||
class Toast extends BaseComponent { | ||
isShown: boolean; | ||
|
||
static get NAME() { | ||
return NAME; | ||
} | ||
|
||
constructor(element: SpiritElement, config?: SpiritConfig) { | ||
super(element, config); | ||
|
||
this.isShown = | ||
this.element.classList.contains(CLASS_NAME_OPEN) || !this.element.classList.contains(CLASS_NAME_HIDDEN); | ||
} | ||
|
||
show() { | ||
if (this.isShown) { | ||
return; | ||
} | ||
|
||
const showEvent = EventHandler.trigger(this.element, Toast.eventName(EVENT_SHOW)) as Event; | ||
if (showEvent.defaultPrevented) { | ||
return; | ||
} | ||
|
||
const dismissEl = SelectorEngine.findOne(SELECTOR_DATA_DISMISS, this.element); | ||
dismissEl?.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; | ||
} | ||
|
||
const dismissEl = SelectorEngine.findOne(SELECTOR_DATA_DISMISS, this.element); | ||
dismissEl?.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,9 @@ | ||
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_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
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