-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
211 lines (177 loc) · 6.69 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { GridHelperBreakpointConfig, GridHelperConfig, SharedGridConfig } from './types';
// Default configuration
const defaultConfig: GridHelperConfig = {
columns: 12,
gutterWidth: '16px',
marginWidth: '16px',
color: 'red',
opacity: 0.1,
key: 'g'
};
/**
* Grid Helper
*
* Provides a grid based on the design guidelines and is helpful for web integration.
*
* - `Control + g` to toggle the grid
*
*/
export default class GridHelper {
private sharedConfig: SharedGridConfig;
private breakpoints: { [key: string]: Partial<GridHelperBreakpointConfig> };
private currentBreakpointConfig: GridHelperBreakpointConfig;
private previousBreakpointConfig: GridHelperBreakpointConfig | null = null;
private gridContainer: HTMLElement;
private isActive: boolean = false;
private ctrlDown: boolean = false;
constructor(config: Partial<GridHelperConfig> = {}) {
const mergedConfig = this.mergeConfig(config, defaultConfig);
this.sharedConfig = mergedConfig;
this.breakpoints = mergedConfig.breakpoints || {};
// Determine initial breakpoint config
this.currentBreakpointConfig = this.getBreakpointConfig();
this.previousBreakpointConfig = this.currentBreakpointConfig;
// Initialize the grid container
this.gridContainer = document.createElement('div');
document.body.append(this.gridContainer);
this.initialize();
}
/**
* Deep merge user config with default config
*/
private mergeConfig(
userConfig: Partial<GridHelperConfig>,
defaultConfig: GridHelperConfig
): GridHelperConfig {
const breakpoints = { ...defaultConfig.breakpoints, ...userConfig.breakpoints };
if (userConfig.breakpoints) {
for (const key in userConfig.breakpoints) {
breakpoints[key] = { ...breakpoints[key], ...userConfig.breakpoints[key] };
}
}
delete userConfig.breakpoints;
const sharedConfig = { ...defaultConfig, ...userConfig };
return {
...sharedConfig,
breakpoints
};
}
/**
* Initialize the grid helper by setting styles, columns, and events.
*/
private initialize() {
this.setGridEvents();
}
/**
* Get the current breakpoint configuration based on the window width.
*/
private getBreakpointConfig(): GridHelperBreakpointConfig {
const width = window.innerWidth;
let matchedBreakpointConfig: Partial<GridHelperBreakpointConfig> = this.sharedConfig;
// Find the closest breakpoint that is less than or equal to the current window width.
for (const [breakpoint, config] of Object.entries(this.breakpoints)) {
if (width >= parseInt(breakpoint)) {
matchedBreakpointConfig = { ...this.sharedConfig, ...config };
}
}
return matchedBreakpointConfig as GridHelperBreakpointConfig;
}
private extractCSSVariable(value: string): string | null {
const match = value.match(/var\((--[\w-]+)\)/);
return match ? match[1] : null;
}
/**
* Set grid container styles.
*/
private setGridHelperStyles() {
const elStyles = this.gridContainer.style;
elStyles.zIndex = '10000';
elStyles.position = 'fixed';
elStyles.top = '0';
elStyles.left = '0';
elStyles.display = 'flex';
elStyles.width = '100%';
elStyles.height = '100%';
elStyles.columnGap = `${this.currentBreakpointConfig.gutterWidth}`;
elStyles.paddingLeft = `${this.currentBreakpointConfig.marginWidth}`;
elStyles.paddingRight = `${this.currentBreakpointConfig.marginWidth}`;
elStyles.pointerEvents = 'none';
elStyles.visibility = this.isActive ? 'visible' : 'hidden';
}
/**
* Set grid columns.
*/
private setGridHelperColumns() {
// Clear previous columns
this.gridContainer.innerHTML = '';
let columns = 0;
// Get the columns value, which can be either a number or a string
let configColumns = this.currentBreakpointConfig.columns;
// If configColumns is a string, check if it's a CSS variable
if (typeof configColumns === 'string') {
const cssVariable = this.extractCSSVariable(configColumns);
if (cssVariable) {
const computedStyle = getComputedStyle(document.documentElement);
const cssValue = computedStyle.getPropertyValue(cssVariable);
// Parse the CSS variable value to an integer
columns = parseInt(cssValue);
// Provide a fallback in case of NaN
if (isNaN(columns)) {
columns = 12; // Default fallback
}
}
} else if (typeof configColumns === 'number') {
columns = configColumns;
}
for (let i = 0; i < columns; i++) {
const column = document.createElement('div');
column.style.flex = '1 1 0';
column.style.backgroundColor = this.currentBreakpointConfig.color;
column.style.opacity = `${this.currentBreakpointConfig.opacity}`;
this.gridContainer.appendChild(column);
}
}
/**
* Set grid events for resize and keydown.
*/
private setGridEvents() {
// Handle resize to update the grid columns and styles
window.addEventListener('resize', () => {
this.currentBreakpointConfig = this.getBreakpointConfig();
this.setGridHelperStyles();
this.setGridHelperColumns();
});
// Toggle grid visibility with keyboard events
document.addEventListener('keydown', (e) => this.handleKeyDown(e));
document.addEventListener('keyup', (e) => this.handleKeyUp(e));
}
/**
* Handle keydown event to toggle the grid.
*/
private handleKeyDown(e: KeyboardEvent) {
if (e.key === 'Control') {
this.ctrlDown = true;
} else if (this.ctrlDown && e.key === this.sharedConfig.key) {
this.currentBreakpointConfig = this.getBreakpointConfig();
this.setGridHelperStyles();
this.setGridHelperColumns();
this.toggleGridVisibility();
}
}
/**
* Handle keyup event to reset Control key state.
*/
private handleKeyUp(e: KeyboardEvent) {
if (e.key === 'Control') {
this.ctrlDown = false;
}
}
/**
* Toggle the visibility of the grid.
*/
private toggleGridVisibility() {
this.gridContainer.style.visibility = this.isActive ? 'hidden' : 'visible';
this.isActive = !this.isActive;
}
}
export * from './types';