Skip to content

Commit

Permalink
Initial toggle fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzy3 committed Dec 12, 2024
1 parent 472f9ae commit 4ef6e78
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libs/designsystem/toggle/src/toggle.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
(blur)="_onInactive()"
(ionChange)="onCheckedChange($event.detail.checked)"
[attr.aria-label]="_ariaLabel"
[justify]="_justify"
[labelPlacement]="_labelPlacement"
>
<ng-content></ng-content>
<span class="default-content">
<ng-content></ng-content>
</span>
</ion-toggle>
15 changes: 15 additions & 0 deletions libs/designsystem/toggle/src/toggle.component.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
@use 'sass:math';
@use 'sass:map';
@use '@kirbydesign/core/src/scss/interaction-state';
@use '@kirbydesign/core/src/scss/utils';

:host {
display: inline-flex;

&:has(ion-toggle.in-item.toggle-justify-space-between) {
width: 100%;
}

// @each $size, $properties in utils.$checkbox-sizes {
// &.#{$size} {
// $vertical-padding: map.get($properties, vertical-padding);

// ion-toggle:not(.in-item)::part(label) {
// padding-block: $vertical-padding;
// }
// }
// }
}

ion-toggle {
Expand Down
19 changes: 19 additions & 0 deletions libs/designsystem/toggle/src/toggle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ export class ToggleComponent implements ControlValueAccessor, OnInit {
) {}

ngOnInit(): void {
/**
* We cannot query ion-toggle for slotted content at this point as the slot has not been rendered.
* But we need to know if content is slotted to set justify and labelPlacement BEFORE ion-toggle is rendered.
* So it has to be done by querying an additional wrapper around the default content slot like this.
*/
this._hasSlottedContent = this.elementRef.nativeElement
.querySelector('.default-content')
.hasChildNodes();

const slot = this.elementRef.nativeElement.getAttribute('slot');
if (slot === 'end' && this._hasSlottedContent) {
this._justify = 'space-between';
this._labelPlacement = 'start';
}

this.inheritAriaAttributes();
}

Expand All @@ -60,6 +75,10 @@ export class ToggleComponent implements ControlValueAccessor, OnInit {

@Output() checkedChange = new EventEmitter<boolean>();

_justify: 'start' | 'end' | 'space-between' = 'start';
_labelPlacement: 'end' | 'fixed' | 'stacked' | 'start' = 'end';
_hasSlottedContent: boolean;

_pressed = false;

onCheckedChange(checked: boolean): void {
Expand Down

0 comments on commit 4ef6e78

Please sign in to comment.