-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathbasic-dropdown-trigger.gts
More file actions
104 lines (100 loc) · 4 KB
/
basic-dropdown-trigger.gts
File metadata and controls
104 lines (100 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { element } from 'ember-element-helper';
import { or } from 'ember-truth-helpers';
import { concat } from '@ember/helper';
import basicDropdownTriggerModifier from '../modifiers/basic-dropdown-trigger.ts';
import { on } from '@ember/modifier';
import { fn } from '@ember/helper';
import type { Dropdown } from './basic-dropdown';
import type { HorizontalPosition, VerticalPosition } from '../types.ts';
export interface BasicDropdownTriggerSignature {
Element: HTMLElement;
Args: {
dropdown?: Dropdown;
eventType?: 'click' | 'mousedown';
stopPropagation?: boolean;
vPosition?: VerticalPosition | null;
hPosition?: HorizontalPosition | null;
defaultClass?: string;
renderInPlace?: boolean;
htmlTag?: keyof HTMLElementTagNameMap;
onBlur?: (dropdown?: Dropdown, event?: FocusEvent) => void;
onClick?: (dropdown?: Dropdown, event?: MouseEvent) => void;
onFocus?: (dropdown?: Dropdown, event?: FocusEvent) => void;
onFocusIn?: (dropdown?: Dropdown, event?: FocusEvent) => void;
onFocusOut?: (dropdown?: Dropdown, event?: FocusEvent) => void;
onKeyDown?: (dropdown?: Dropdown, event?: KeyboardEvent) => void;
onMouseDown?: (dropdown?: Dropdown, event?: MouseEvent) => void;
onMouseEnter?: (dropdown?: Dropdown, event?: MouseEvent) => void;
onMouseLeave?: (dropdown?: Dropdown, event?: MouseEvent) => void;
onTouchEnd?: (dropdown?: Dropdown, event?: TouchEvent) => void;
};
Blocks: {
default: [];
};
}
export default class BasicDropdownTrigger extends Component<BasicDropdownTriggerSignature> {
// Actions
/**
* Allows similar behavior to `ember-composable-helpers`' `optional` helper.
* Avoids adding extra dependencies.
* Can be removed when the template `V1` compatibility event handlers are removed.
*
* @see https://github.com/cibernox/ember-basic-dropdown/issues/498
* @memberof BasicDropdownContent
*/
noop(): void {}
@action
disableDocumentTextSelect(disable: boolean) {
if (disable) {
document.body.classList.add('ember-basic-dropdown-text-select-disabled');
} else {
document.body.classList.remove(
'ember-basic-dropdown-text-select-disabled',
);
}
}
<template>
{{#if @dropdown}}
{{#let (element (or @htmlTag "div")) as |OptionalTag|}}
{{! template-lint-disable no-pointer-down-event-binding }}
<OptionalTag
class="ember-basic-dropdown-trigger
{{if @renderInPlace ' ember-basic-dropdown-trigger--in-place'}}
{{if
@hPosition
(concat ' ember-basic-dropdown-trigger--' @hPosition)
}}
{{if
@vPosition
(concat ' ember-basic-dropdown-trigger--' @vPosition)
}}
{{@defaultClass}}"
{{basicDropdownTriggerModifier
dropdown=@dropdown
eventType=@eventType
stopPropagation=@stopPropagation
}}
tabindex={{unless @dropdown.disabled "0"}}
...attributes
{{on "mousedown" (fn this.disableDocumentTextSelect true)}}
{{on "mouseup" (fn this.disableDocumentTextSelect false)}}
{{! V1 compatibility - See #498 }}
{{on "keydown" (fn (or @onKeyDown this.noop) @dropdown)}}
{{on "mousedown" (fn (or @onMouseDown this.noop) @dropdown)}}
{{on "touchend" (fn (or @onTouchEnd this.noop) @dropdown)}}
{{on "click" (fn (or @onClick this.noop) @dropdown)}}
{{on "mouseenter" (fn (or @onMouseEnter this.noop) @dropdown)}}
{{on "mouseleave" (fn (or @onMouseLeave this.noop) @dropdown)}}
{{on "focus" (fn (or @onFocus this.noop) @dropdown)}}
{{on "blur" (fn (or @onBlur this.noop) @dropdown)}}
{{on "focusin" (fn (or @onFocusIn this.noop) @dropdown)}}
{{on "focusout" (fn (or @onFocusOut this.noop) @dropdown)}}
>
{{yield}}
</OptionalTag>
{{/let}}
{{/if}}
</template>
}