Skip to content

Commit af8b1fc

Browse files
committed
fix: resize ghost refinement + remove tile related stuff from resize controller
1 parent b3ff5f7 commit af8b1fc

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

src/components/tile-manager/resize-controller.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { ReactiveController, ReactiveControllerHost } from 'lit';
22
import type { Ref } from 'lit/directives/ref.js';
3-
import {
4-
findElementFromEventPath,
5-
getElementByIdFromRoot,
6-
} from '../common/util.js';
3+
import { findElementFromEventPath } from '../common/util.js';
74

85
export type ResizeMode = 'immediate' | 'deferred';
96

@@ -14,6 +11,7 @@ export type ResizeCallbackParams = {
1411
current: DOMRect;
1512
dx: number;
1613
dy: number;
14+
ghost: HTMLElement | null;
1715
};
1816
};
1917

@@ -60,11 +58,6 @@ class ResizeController implements ReactiveController {
6058
protected _initialState!: DOMRect;
6159
private _state!: DOMRect;
6260

63-
private _initialPointerX!: number;
64-
private _initialPointerY!: number;
65-
private _minWidth!: string;
66-
private _minHeight!: string;
67-
6861
protected get _element() {
6962
return this._config?.ref ? this._config.ref.value! : this._host;
7063
}
@@ -105,16 +98,7 @@ class ResizeController implements ReactiveController {
10598
const rect = resizableElement!.getBoundingClientRect();
10699
this._initialState = structuredClone(rect);
107100
this._state = rect;
108-
this._initialPointerX = event.clientX;
109-
this._initialPointerY = event.clientY;
110101
this._id = event.pointerId;
111-
112-
this._minWidth = getComputedStyle(resizableElement!).getPropertyValue(
113-
'--ig-min-col-width'
114-
);
115-
this._minHeight = getComputedStyle(resizableElement!).getPropertyValue(
116-
'--ig-min-row-height'
117-
);
118102
}
119103

120104
private _createCallbackParams(event: PointerEvent): ResizeCallbackParams {
@@ -125,6 +109,7 @@ class ResizeController implements ReactiveController {
125109
current: this._state,
126110
dx: this._state.width - this._initialState.width,
127111
dy: this._state.height - this._initialState.height,
112+
ghost: this._ghost,
128113
},
129114
};
130115
}
@@ -152,10 +137,11 @@ class ResizeController implements ReactiveController {
152137

153138
if (this._config?.start) {
154139
this._setInitialState(event);
155-
this._config.start.call(this._host, this._createCallbackParams(event));
156-
157140
this._createGhost();
158141

142+
const params = this._createCallbackParams(event);
143+
this._config.start.call(this._host, params);
144+
159145
this._element.setPointerCapture(this._id);
160146
this._toggleSubsequentEvents(true);
161147
}
@@ -169,19 +155,9 @@ class ResizeController implements ReactiveController {
169155
// REVIEW: Sequencing
170156

171157
if (this._config?.resize) {
172-
const deltaX = event.clientX - this._initialPointerX;
173-
const deltaY = event.clientY - this._initialPointerY;
174-
175-
this._state.width = Math.max(
176-
this._initialState.width + deltaX,
177-
Number.parseFloat(this._minWidth)
178-
);
179-
this._state.height = Math.max(
180-
this._initialState.height + deltaY,
181-
Number.parseFloat(this._minHeight)
182-
);
183-
184-
this._config.resize.call(this._host, this._createCallbackParams(event));
158+
const params = this._createCallbackParams(event);
159+
this._config.resize.call(this._host, params);
160+
this._state = params.state.current;
185161
}
186162

187163
const target = this._config.mode === 'deferred' ? this._ghost! : this._host;

src/components/tile-manager/resize-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ export default class IgcResizeComponent extends EventEmitterMixin<
8989

9090
private _handleResizeStart(params: ResizeCallbackParams) {
9191
params.event.preventDefault();
92+
this.emitEvent('igcResizeStart', { bubbles: false, detail: params });
9293

9394
this._isResizing = true;
9495
this._setState(params.state);
9596
this._adorner.value!.focus();
96-
this.emitEvent('igcResizeStart', { bubbles: false, detail: params });
9797
}
9898

9999
private _handleResize(params: ResizeCallbackParams) {

src/components/tile-manager/tile.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ export default class IgcTileComponent extends EventEmitterMixin<
6868
private _disableDrag = false;
6969
private _fullscreen = false;
7070
private _maximized = false;
71+
private _initialPointerX: number | null = null;
72+
private _initialPointerY: number | null = null;
73+
private _cachedStyles: {
74+
ghostBackground?: string;
75+
ghostBorder?: string;
76+
ghostBorderRadius?: string;
77+
ghostMinWidth?: string;
78+
ghostMinHeight?: string;
79+
} = {};
7180
private _context = new ContextProvider(this, {
7281
context: tileContext,
7382
initialValue: this,
@@ -333,9 +342,43 @@ export default class IgcTileComponent extends EventEmitterMixin<
333342
this._isDragging = false;
334343
}
335344

336-
private _handleResize(_: CustomEvent<ResizeCallbackParams>) {
345+
private cacheStyles() {
346+
const computedStyle = window.getComputedStyle(this);
347+
348+
this._cachedStyles = {
349+
ghostBackground: computedStyle.getPropertyValue(
350+
'--placeholder-background'
351+
),
352+
ghostBorder: computedStyle.getPropertyValue('--ghost-border'),
353+
ghostBorderRadius: computedStyle.getPropertyValue('--border-radius'),
354+
ghostMinWidth: computedStyle.getPropertyValue('--ig-min-col-width'),
355+
ghostMinHeight: computedStyle.getPropertyValue('--ig-min-row-height'),
356+
};
357+
}
358+
359+
private _handleResizeStart(event: CustomEvent<ResizeCallbackParams>) {
360+
const ghostElement = event.detail.state.ghost;
361+
this._initialPointerX = event.detail.event.clientX;
362+
this._initialPointerY = event.detail.event.clientY;
363+
364+
if (ghostElement) {
365+
ghostElement.style.minWidth = this._cachedStyles.ghostMinWidth!;
366+
ghostElement.style.minHeight = this._cachedStyles.ghostMinHeight!;
367+
}
368+
}
369+
370+
private _handleResize(event: CustomEvent<ResizeCallbackParams>) {
337371
this._isResizing = true;
338-
// console.log(event.detail.state);
372+
373+
const ghostElement = event.detail.state.current;
374+
375+
if (ghostElement) {
376+
const deltaX = event.detail.event.clientX - this._initialPointerX!;
377+
const deltaY = event.detail.event.clientY - this._initialPointerY!;
378+
379+
ghostElement.width = event.detail.state.initial.width + deltaX;
380+
ghostElement.height = event.detail.state.initial.height + deltaY;
381+
}
339382
}
340383

341384
private _handleResizeEnd(event: CustomEvent<ResizeCallbackParams>) {
@@ -360,43 +403,26 @@ export default class IgcTileComponent extends EventEmitterMixin<
360403
});
361404

362405
this._isResizing = false;
406+
this._initialPointerX = null;
407+
this._initialPointerY = null;
408+
this._cachedStyles = {};
363409
}
364410

365411
// REVIEW
366412
protected ghostFactory = () => {
367-
const ghostBackground = window
368-
.getComputedStyle(this)
369-
.getPropertyValue('--placeholder-background');
370-
371-
const ghostBorder = window
372-
.getComputedStyle(this)
373-
.getPropertyValue('--ghost-border');
374-
375-
const ghostBorderRadius = window
376-
.getComputedStyle(this)
377-
.getPropertyValue('--border-radius');
378-
379-
const ghostMinWidth = window
380-
.getComputedStyle(this)
381-
.getPropertyValue('--ig-min-col-width');
382-
383-
const ghostMinHeight = window
384-
.getComputedStyle(this)
385-
.getPropertyValue('--ig-min-row-height');
413+
this.cacheStyles();
386414

387415
const ghost = document.createElement('div');
388416
Object.assign(ghost.style, {
389417
position: 'absolute',
390418
top: 0,
391419
left: 0,
392420
zIndex: 1000,
393-
background: ghostBackground,
394-
border: `1px solid ${ghostBorder}`,
395-
borderRadius: ghostBorderRadius,
421+
background: this._cachedStyles.ghostBackground,
422+
border: `1px solid ${this._cachedStyles.ghostBorder}`,
423+
borderRadius: this._cachedStyles.ghostBorderRadius,
396424
width: '100%',
397425
height: '100%',
398-
minWidth: ghostMinWidth,
399-
minHeight: ghostMinHeight,
400426
gridRow: '',
401427
gridColumn: '',
402428
});
@@ -444,6 +470,7 @@ export default class IgcTileComponent extends EventEmitterMixin<
444470
part="resize"
445471
.ghostFactory=${this.ghostFactory}
446472
mode="deferred"
473+
@igcResizeStart=${this._handleResizeStart}
447474
@igcResize=${this._handleResize}
448475
@igcResizeEnd=${this._handleResizeEnd}
449476
>

0 commit comments

Comments
 (0)