@tmagic/stage 1.0.0-beta.1 → 1.0.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.ts CHANGED
@@ -16,14 +16,9 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
+ import { Mode, SELECTED_CLASS } from './const';
19
20
  import type { Offset } from './types';
20
21
 
21
- export enum Mode {
22
- ABSOLUTE = 'absolute',
23
- FIXED = 'fixed',
24
- SORTABLE = 'sortable',
25
- }
26
-
27
22
  export const getOffset = (el: HTMLElement): Offset => {
28
23
  const { transform } = getComputedStyle(el);
29
24
  const { offsetParent } = el;
@@ -113,8 +108,63 @@ export const isFixed = (el?: HTMLElement): boolean => {
113
108
  return getComputedStyle(el).position === 'fixed';
114
109
  };
115
110
 
111
+ export const isFixedParent = (el: HTMLElement) => {
112
+ let fixed = false;
113
+ let dom = el;
114
+ while (dom) {
115
+ fixed = isFixed(dom);
116
+ if (fixed) {
117
+ break;
118
+ }
119
+ const { parentElement } = dom;
120
+ if (!parentElement || parentElement.tagName === 'BODY') {
121
+ break;
122
+ }
123
+ dom = parentElement;
124
+ }
125
+ return fixed;
126
+ };
127
+
116
128
  export const getMode = (el: HTMLElement): Mode => {
117
- if (isFixed(el)) return Mode.FIXED;
129
+ if (isFixedParent(el)) return Mode.FIXED;
118
130
  if (isStatic(el) || isRelative(el)) return Mode.SORTABLE;
119
131
  return Mode.ABSOLUTE;
120
132
  };
133
+
134
+ export const getScrollParent = (element: HTMLElement, includeHidden = false): HTMLElement | null => {
135
+ let style = getComputedStyle(element);
136
+ const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
137
+
138
+ if (isFixed(element)) return null;
139
+ for (let parent = element; parent.parentElement; ) {
140
+ parent = parent.parentElement;
141
+ style = getComputedStyle(parent);
142
+ if (isAbsolute(element) && isStatic(element)) {
143
+ continue;
144
+ }
145
+ if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;
146
+ }
147
+
148
+ return null;
149
+ };
150
+
151
+ export const createDiv = ({ className, cssText }: { className: string; cssText: string }) => {
152
+ const el = globalThis.document.createElement('div');
153
+ el.className = className;
154
+ el.style.cssText = cssText;
155
+ return el;
156
+ };
157
+
158
+ export const removeSelectedClassName = (doc: Document) => {
159
+ const oldEl = doc.querySelector(`.${SELECTED_CLASS}`);
160
+
161
+ if (oldEl) {
162
+ oldEl.classList.remove(SELECTED_CLASS);
163
+ (oldEl.parentNode as HTMLDivElement)?.classList.remove(`${SELECTED_CLASS}-parent`);
164
+ }
165
+ };
166
+
167
+ export const addSelectedClassName = (el: Element) => {
168
+ el.classList.add(SELECTED_CLASS);
169
+ (el.parentNode as Element)?.classList.add(`${SELECTED_CLASS}-parent`);
170
+ };