Skip to content

Commit 7eafda0

Browse files
feat(overlay, toggle): add offset type parameter to setOffset method
1 parent cb3367c commit 7eafda0

File tree

8 files changed

+74
-15
lines changed

8 files changed

+74
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes for each version of this project will be documented in this
66
### New Features
77
- `IgxCombo`, `IgxSimpleCombo`:
88
- Introduced abillity for hiding the clear icon button when the custom clear icon template is empty.
9+
- `IgxOverlayService`, `IgxToggleDirective`:
10+
- Added an optional `offsetMode` parameter to the `setOffset` method that determines whether to add (by default) or set the offset values using `OffsetMode.Add` and `OffsetMode.Set`.
911

1012
## 17.2.0
1113
### New Features
@@ -120,6 +122,7 @@ All notable changes for each version of this project will be documented in this
120122
- `IgxCombo`,`IgxSimpleCombo`
121123
- **Breaking Change** The `displayValue` property now returns the display text as expected (instead of display values in array).
122124

125+
=======
123126
## 16.1.5
124127
### General
125128
- `IgxButtonGroup`:

projects/igniteui-angular/src/lib/directives/toggle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ handlers when the toggle is opened and respectively closed.
5252
| `close` | --- | `void` | Closes the toggle. |
5353
| `toggle` | overlaySettings?: `OverlaySettings` | `void` | Closes the toggle. |
5454
| `reposition` | --- | `void` | Repositions the toggle. |
55-
| `setOffset` | Offsets the content's position along the relevant axis precisely by the specified amount. |deltaX, deltaY |
55+
| `setOffset` | Offsets the content along the corresponding axis by the provided amount with optional offsetMode that determines whether to add (by default) or set the offset values with OffsetMode.Add and OffsetMode.Set. | deltaX, deltaY, offsetMode? |
5656

5757

5858

projects/igniteui-angular/src/lib/directives/toggle/toggle.directive.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CancelableEventArgs } from '../../core/utils';
1111

1212
import { configureTestSuite } from '../../test-utils/configure-suite';
1313
import { first } from 'rxjs/operators';
14+
import { OffsetMode } from '../../services/overlay/utilities';
1415

1516
describe('IgxToggle', () => {
1617
configureTestSuite();
@@ -213,6 +214,34 @@ describe('IgxToggle', () => {
213214
expect(toggle.closed.emit).toHaveBeenCalled();
214215
}));
215216

217+
it('should offset the toggle content correctly using setOffset method and optional offsetMode', () => {
218+
const fixture = TestBed.createComponent(IgxToggleActionTestComponent);
219+
const component = fixture.componentInstance;
220+
const toggle = component.toggle;
221+
const overlayId = toggle.overlayId;
222+
fixture.detectChanges();
223+
224+
const overlayService = TestBed.inject(IgxOverlayService);
225+
spyOn(overlayService, 'setOffset').and.callThrough();
226+
fixture.detectChanges();
227+
228+
// Set initial offset
229+
toggle.setOffset(20, 20);
230+
expect(overlayService.setOffset).toHaveBeenCalledWith(overlayId, 20, 20, undefined);
231+
232+
// Add offset values to the existing ones (default behavior)
233+
toggle.setOffset(10, 10);
234+
expect(overlayService.setOffset).toHaveBeenCalledWith(overlayId, 10, 10, undefined);
235+
236+
// Add offset values using OffsetMode.Add
237+
toggle.setOffset(20, 20, OffsetMode.Add);
238+
expect(overlayService.setOffset).toHaveBeenCalledWith(overlayId, 20, 20, OffsetMode.Add);
239+
240+
// Set offset values using OffsetMode.Set
241+
toggle.setOffset(10, 10, OffsetMode.Set);
242+
expect(overlayService.setOffset).toHaveBeenCalledWith(overlayId, 10, 10, OffsetMode.Set);
243+
});
244+
216245
it('Toggle should be registered into navigationService if it is passed through identifier', fakeAsync(() => {
217246
const fixture = TestBed.createComponent(IgxToggleServiceInjectComponent);
218247
fixture.detectChanges();

projects/igniteui-angular/src/lib/directives/toggle/toggle.directive.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { filter, first, takeUntil } from 'rxjs/operators';
1919
import { IgxNavigationService, IToggleView } from '../../core/navigation';
2020
import { IgxOverlayService } from '../../services/overlay/overlay';
2121
import { IPositionStrategy } from '../../services/overlay/position/IPositionStrategy';
22-
import { OverlayClosingEventArgs, OverlayEventArgs, OverlaySettings } from '../../services/overlay/utilities';
22+
import { OffsetMode, OverlayClosingEventArgs, OverlayEventArgs, OverlaySettings } from '../../services/overlay/utilities';
2323
import { Subscription, Subject, MonoTypeOperatorFunction } from 'rxjs';
2424

2525
export interface ToggleViewEventArgs extends IBaseEventArgs {
@@ -318,10 +318,11 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
318318
}
319319

320320
/**
321-
* Offsets the content along the corresponding axis by the provided amount
321+
* Offsets the content along the corresponding axis by the provided amount with optional
322+
* offsetMode that determines whether to add (by default) or set the offset values with OffsetMode.Add and OffsetMode.Set
322323
*/
323-
public setOffset(deltaX: number, deltaY: number) {
324-
this.overlayService.setOffset(this._overlayId, deltaX, deltaY);
324+
public setOffset(deltaX: number, deltaY: number, offsetMode?: OffsetMode) {
325+
this.overlayService.setOffset(this._overlayId, deltaX, deltaY, offsetMode);
325326
}
326327

327328
/**

projects/igniteui-angular/src/lib/services/overlay/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ this.overlay.show(component, overlaySettings);
9999
|detach | Remove overlay with the provided id |id |
100100
|detachAll | Remove all the overlays |- |
101101
|reposition | Repositions the native element of the component with provided id |id |
102-
|setOffset | Offsets the content's position along the relevant axis precisely by the specified amount |id, deltaX, deltaY |
102+
|setOffset | Offsets the content along the corresponding axis by the provided amount with optional offsetMode that determines whether to add (by default) or set the offset values with OffsetMode.Add and OffsetMode.Set | id, deltaX, deltaY, offsetMode? |
103103

104104
###### IPositionStrategy
105105

projects/igniteui-angular/src/lib/services/overlay/overlay.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { CloseScrollStrategy } from './scroll/close-scroll-strategy';
3434
import { NoOpScrollStrategy } from './scroll/NoOpScrollStrategy';
3535
import {
3636
HorizontalAlignment,
37+
OffsetMode,
3738
OverlayCancelableEventArgs,
3839
OverlayEventArgs,
3940
OverlaySettings,
@@ -820,6 +821,7 @@ describe('igxOverlay', () => {
820821
const componentElementRect = componentElement.getBoundingClientRect();
821822
let overlayContentTransform = contentElement.style.transform;
822823
const firstTransform = 'translate(40px, 40px)';
824+
const secondTransform = 'translate(30px, 60px)';
823825

824826
expect(contentElementRect.top).toEqual(componentElementRect.top);
825827
expect(contentElementRect.left).toEqual(componentElementRect.left);
@@ -832,7 +834,6 @@ describe('igxOverlay', () => {
832834
const contentElementRectNew = contentElement.getBoundingClientRect();
833835
const componentElementRectNew = componentElement.getBoundingClientRect();
834836
overlayContentTransform = contentElement.style.transform;
835-
const secondTransform = 'translate(-10px, 20px)';
836837

837838
expect(contentElementRectNew.top).toEqual(componentElementRectNew.top);
838839
expect(contentElementRectNew.left).toEqual(componentElementRectNew.left);
@@ -847,7 +848,7 @@ describe('igxOverlay', () => {
847848
overlayInstance.detachAll();
848849
}));
849850

850-
it('Should set transformX and transformY correctly in setOffset method', fakeAsync(() => {
851+
it('Should offset the overlay content correctly using setOffset method and optional offsetMode', fakeAsync(() => {
851852
const fixture = TestBed.createComponent(WidthTestOverlayComponent);
852853
const overlayInstance = fixture.componentInstance.overlay;
853854

@@ -862,8 +863,18 @@ describe('igxOverlay', () => {
862863
expect(overlayInfo.transformX).toEqual(20);
863864
expect(overlayInfo.transformY).toEqual(20);
864865

865-
// Set new offset
866+
// Add offset values to the existing ones (default behavior)
866867
overlayInstance.setOffset(id, 10, 10);
868+
expect(overlayInfo.transformX).toEqual(30);
869+
expect(overlayInfo.transformY).toEqual(30);
870+
871+
// Add offset values using OffsetMode.Add
872+
overlayInstance.setOffset(id, 20, 20, OffsetMode.Add);
873+
expect(overlayInfo.transformX).toEqual(50);
874+
expect(overlayInfo.transformY).toEqual(50);
875+
876+
// Set offset values using OffsetMode.Set
877+
overlayInstance.setOffset(id, 10, 10, OffsetMode.Set);
867878
expect(overlayInfo.transformX).toEqual(10);
868879
expect(overlayInfo.transformY).toEqual(10);
869880

projects/igniteui-angular/src/lib/services/overlay/overlay.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import {
4242
PositionSettings,
4343
RelativePosition,
4444
RelativePositionStrategy,
45-
VerticalAlignment
45+
VerticalAlignment,
46+
OffsetMode
4647
} from './utilities';
4748
import { fadeIn, fadeOut, IAnimationParams, scaleInHorLeft, scaleInHorRight, scaleInVerBottom, scaleInVerTop, scaleOutHorLeft, scaleOutHorRight, scaleOutVerBottom, scaleOutVerTop, slideInBottom, slideInTop, slideOutBottom, slideOutTop } from 'igniteui-angular/animations';
4849

@@ -489,24 +490,30 @@ export class IgxOverlayService implements OnDestroy {
489490
}
490491

491492
/**
492-
* Offsets the content along the corresponding axis by the provided amount
493+
* Offsets the content along the corresponding axis by the provided amount with optional offsetMode that determines whether to add (by default) or set the offset values
493494
*
494495
* @param id Id to offset overlay for
495496
* @param deltaX Amount of offset in horizontal direction
496497
* @param deltaY Amount of offset in vertical direction
498+
* @param offsetMode Determines whether to add (by default) or set the offset values with OffsetMode.Add and OffsetMode.Set
497499
* ```typescript
498-
* this.overlay.setOffset(id, deltaX, deltaY);
500+
* this.overlay.setOffset(id, deltaX, deltaY, offsetMode);
499501
* ```
500502
*/
501-
public setOffset(id: string, deltaX: number, deltaY: number) {
503+
public setOffset(id: string, deltaX: number, deltaY: number, offsetMode?: OffsetMode) {
502504
const info: OverlayInfo = this.getOverlayById(id);
503505

504506
if (!info) {
505507
return;
506508
}
507509

508-
info.transformX = deltaX;
509-
info.transformY = deltaY;
510+
if (offsetMode === OffsetMode.Set) {
511+
info.transformX = deltaX;
512+
info.transformY = deltaY;
513+
} else {
514+
info.transformX += deltaX;
515+
info.transformY += deltaY;
516+
}
510517

511518
const transformX = info.transformX;
512519
const transformY = info.transformY;

projects/igniteui-angular/src/lib/services/overlay/utilities.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export enum AbsolutePosition {
4747
Center = 'center'
4848
}
4949

50+
/**
51+
* Determines whether to add or set the offset values.
52+
*/
53+
export enum OffsetMode {
54+
Add,
55+
Set
56+
}
57+
5058
// TODO: make this interface
5159
export class Point {
5260
constructor(public x: number, public y: number) { }

0 commit comments

Comments
 (0)