forked from materializecss/materialize
-
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.
feature(DockedDisplayPlugin) implemented docked display plugin materi…
- Loading branch information
1 parent
65ddf18
commit 9eaa469
Showing
3 changed files
with
180 additions
and
7 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,87 @@ | ||
import { Utils } from '../utils'; | ||
|
||
export interface DockedDisplayPluginOptions { | ||
/** | ||
* Margin between element and docked container | ||
*/ | ||
margin: number, | ||
/** | ||
* Transition movement | ||
*/ | ||
transition: number, | ||
/** | ||
* Transition duration | ||
*/ | ||
duration: number | ||
} | ||
|
||
const _defaults: DockedDisplayPluginOptions = { | ||
margin: 5, | ||
transition: 10, | ||
duration: 250 | ||
} | ||
|
||
export class DockedDisplayPlugin { | ||
private readonly el: HTMLElement; | ||
private readonly container: HTMLDivElement; | ||
private options: Partial<DockedDisplayPluginOptions>; | ||
private visible: boolean; | ||
|
||
constructor(el: HTMLElement, container: HTMLElement, options: Partial<DockedDisplayPluginOptions>) { | ||
this.el = el; | ||
this.options = { | ||
..._defaults, | ||
...options | ||
}; | ||
|
||
this.container = document.createElement('div'); | ||
this.container.classList.add('display-docked'); | ||
this.container.append(container); | ||
el.parentElement.append(this.container); | ||
|
||
document.addEventListener('click', (e) => { | ||
if (this.visible && !(this.el === <HTMLElement>e.target) && !((<HTMLElement>e.target).closest('.display-docked'))) { | ||
this.hide(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Initializes instance of DockedDisplayPlugin | ||
* @param el HTMLElement to position to | ||
* @param container HTMLElement to be positioned | ||
* @param options Plugin options | ||
*/ | ||
static init(el: HTMLElement, container: HTMLElement, options?: Partial<DockedDisplayPluginOptions>): DockedDisplayPlugin { | ||
return new DockedDisplayPlugin(el, container, options); | ||
} | ||
|
||
show = () => { | ||
if (this.visible) return; | ||
this.visible = true; | ||
const coordinates = Utils._setAbsolutePosition(this.el, this.container, 'bottom', this.options.margin, this.options.transition); | ||
this.container.style.left = '16px'; | ||
// @todo move to Util? -> duplicate code fragment with tooltip | ||
// easeOutCubic | ||
this.container.style.transition = ` | ||
transform ${this.options.duration}ms ease-out, | ||
opacity ${this.options.duration}ms ease-out`; | ||
setTimeout(() => { | ||
this.container.style.transform = `translateX(${coordinates.x}px) translateY(${coordinates.y}px)`; | ||
this.container.style.opacity = (1).toString(); | ||
}, 1); | ||
}; | ||
|
||
hide = () => { | ||
if (!this.visible) return; | ||
this.visible = false; | ||
// @todo move to Util? -> duplicate code fragment with tooltip | ||
this.container.style.transition = ` | ||
transform ${this.options.duration}ms ease-out, | ||
opacity ${this.options.duration}ms ease-out`; | ||
setTimeout(() => { | ||
this.container.style.transform = `translateX(0px) translateY(0px)`; | ||
this.container.style.opacity = '0'; | ||
}, 1); | ||
}; | ||
} |
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