-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.ts
189 lines (156 loc) · 5.07 KB
/
style.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { CSSProperties, Direction, Options, isHorizontal, isVertical, Theme, Inline } from './types'
const directionToRotation = {
[Direction.left]: 180,
[Direction.right]: 0,
[Direction.top]: 270,
[Direction.bottom]: 90,
}
// Apply style object as inline-style to an element.
const add = (element: HTMLElement, properties: CSSProperties) => {
Object.keys(properties).forEach((property) => {
element.style[property] = properties[property]
})
}
const alignment = (direction: Direction, options: Options, observer = false) => {
const style: CSSProperties = {}
const horizontal = isHorizontal(direction)
const vertical = isVertical(direction)
if (horizontal) {
style.top = '0'
}
if (vertical) {
style.left = '0'
}
// Minimally possible observer width.
const size = observer ? '1px' : options.width
style.width = horizontal ? size : '100%'
style.height = vertical ? size : '100%'
return style
}
export const base = {
indicator: (_: HTMLElement, options: Options, direction: Direction) => {
const style: CSSProperties = {
position: 'absolute',
// Initially hidden.
display: 'none',
cursor: options.click ? 'pointer' : 'inherit',
[direction]: '0',
}
if (isHorizontal(direction)) {
style.alignItems =
options.arrow && options.arrow.position !== 'center'
? `flex-${options.arrow.position}`
: 'center'
style.justifyContent = 'center'
} else {
style.justifyContent =
options.arrow && options.arrow.position !== 'center'
? `flex-${options.arrow.position}`
: 'center'
style.alignItems = 'center'
}
return { ...style, ...alignment(direction, options) }
},
hide: (indicator: HTMLSpanElement) => {
indicator.style.display = 'none'
},
show: (indicator: HTMLSpanElement) => {
indicator.style.display = 'flex'
},
arrow: (_: HTMLElement, __: Options, direction: Direction) => ({
width: '12px',
height: '12px',
display: 'block',
transform: `rotate(${directionToRotation[direction]}deg)`,
}),
outerWrapper: (_: HTMLElement, __: Options, ___: boolean, inline: Inline) => {
const styles: CSSProperties = { position: 'relative' }
if (inline) {
styles.display = inline === 'inline' ? 'inline-block' : inline
}
return styles
},
element: (element: HTMLElement, _: Options, table: boolean, inline: Inline) => {
const styles: CSSProperties = {}
if (inline) {
styles.display = inline === 'inline' ? 'inline-block' : inline
styles.verticalAlign = 'top'
}
// Make element scrollable.
if (!table && element.style.overflow !== 'scroll') {
styles.overflow = 'auto'
}
if (table) {
styles.position = 'relative'
styles.display = 'inline-block'
styles.verticalAlign = 'top'
}
return styles
},
innerWrapper: (element: HTMLElement, _: Options, table: boolean) => {
const styles: CSSProperties = {
position: 'relative',
display: 'inline-flex',
// Avoid small bottom space otherwise added by inline-.
verticalAlign: 'top',
}
if (table && element && element.style.overflow !== 'scroll') {
styles.overflow = 'auto'
}
return styles
},
observer: (_: HTMLElement, options: Options, direction: Direction) =>
({
position: 'absolute',
pointerEvents: 'none',
[direction]: '0',
...alignment(direction, options, true),
}) as CSSProperties,
}
type ThemeKey = keyof Theme
const apply = (
method: ((...args: any[]) => CSSProperties | void) | CSSProperties,
...args: any[]
) => {
if (typeof method === 'object') {
return method
}
const result = method(...args)
if (typeof result === 'object') {
return result
}
return undefined
}
// Base theme usually applied as required for plugin to work.
// show and hide will not be extended when overridden by theme.
const applyBaseTheme = (key: ThemeKey, element: HTMLElement, options: Options, args: any[]) => {
if (['show', 'hide'].includes(key) && options.theme[key]) {
return {}
}
return apply(base[key], element, options, ...args)
}
export const theme = (element: HTMLElement, key: ThemeKey, options: Options, ...args: any[]) => {
let userProperties: CSSProperties | undefined
if (options.theme && options.theme[key]) {
userProperties = apply(options.theme[key], element, options, ...args)
}
let baseProperties = applyBaseTheme(key, element, options, args)
// Add simple user styles from options.
if (typeof options.inlineStyles === 'object' && typeof options.inlineStyles[key] === 'object') {
baseProperties = { ...baseProperties, ...options.inlineStyles[key] }
}
// Add default theme or user theme.
const styles = userProperties ? { ...baseProperties, ...userProperties } : baseProperties
if (styles && Object.keys(styles).length) {
add(element, styles)
}
}
export const hideScrollbar = (element: HTMLElement) => {
add(element, {
// Hide scrollbar in IE and Edge.
// @ts-ignore
'-ms-overflow-style': 'none',
// Hide scrollbar in Firefox.
'scrollbar-width': 'none',
})
}