@sonny-ui/core 0.1.0-alpha.1 → 0.1.0-alpha.10

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.
Files changed (198) hide show
  1. package/README.md +101 -32
  2. package/fesm2022/sonny-ui-core.mjs +3031 -42
  3. package/fesm2022/sonny-ui-core.mjs.map +1 -1
  4. package/package.json +8 -5
  5. package/schematics/ng-add/schema.json +1 -1
  6. package/schematics/ng-generate/component/index.js +1 -1
  7. package/schematics/ng-generate/component/schema.json +1 -1
  8. package/src/lib/accordion/accordion.directives.spec.ts +173 -0
  9. package/src/lib/accordion/accordion.directives.ts +147 -0
  10. package/src/lib/accordion/index.ts +8 -0
  11. package/src/lib/alert/alert.directives.spec.ts +154 -0
  12. package/src/lib/alert/alert.directives.ts +70 -0
  13. package/src/lib/alert/alert.variants.ts +25 -0
  14. package/src/lib/alert/index.ts +6 -0
  15. package/src/lib/avatar/avatar.component.spec.ts +75 -0
  16. package/src/lib/avatar/avatar.component.ts +44 -0
  17. package/src/lib/avatar/avatar.variants.ts +26 -0
  18. package/src/lib/avatar/index.ts +2 -0
  19. package/src/lib/badge/badge.directive.spec.ts +74 -0
  20. package/src/lib/badge/badge.directive.ts +18 -0
  21. package/src/lib/badge/badge.variants.ts +29 -0
  22. package/src/lib/badge/index.ts +2 -0
  23. package/src/lib/breadcrumb/breadcrumb.directives.spec.ts +80 -0
  24. package/src/lib/breadcrumb/breadcrumb.directives.ts +84 -0
  25. package/src/lib/breadcrumb/index.ts +8 -0
  26. package/src/lib/button/button.directive.spec.ts +92 -0
  27. package/src/lib/button/button.directive.ts +29 -0
  28. package/src/lib/button/button.variants.ts +30 -0
  29. package/src/lib/button/index.ts +2 -0
  30. package/src/lib/button-group/button-group.directive.spec.ts +46 -0
  31. package/src/lib/button-group/button-group.directive.ts +20 -0
  32. package/src/lib/button-group/button-group.variants.ts +18 -0
  33. package/src/lib/button-group/index.ts +2 -0
  34. package/src/lib/calendar/calendar.component.spec.ts +105 -0
  35. package/src/lib/calendar/calendar.component.ts +231 -0
  36. package/src/lib/calendar/index.ts +1 -0
  37. package/src/lib/card/card.directives.spec.ts +104 -0
  38. package/src/lib/card/card.directives.ts +78 -0
  39. package/src/lib/card/card.variants.ts +28 -0
  40. package/src/lib/card/index.ts +9 -0
  41. package/src/lib/carousel/carousel.directives.spec.ts +85 -0
  42. package/src/lib/carousel/carousel.directives.ts +164 -0
  43. package/src/lib/carousel/index.ts +8 -0
  44. package/src/lib/chat-bubble/chat-bubble.directives.spec.ts +52 -0
  45. package/src/lib/chat-bubble/chat-bubble.directives.ts +102 -0
  46. package/src/lib/chat-bubble/index.ts +11 -0
  47. package/src/lib/checkbox/checkbox.directive.spec.ts +57 -0
  48. package/src/lib/checkbox/checkbox.directive.ts +17 -0
  49. package/src/lib/checkbox/checkbox.variants.ts +19 -0
  50. package/src/lib/checkbox/index.ts +2 -0
  51. package/src/lib/combobox/combobox.component.spec.ts +151 -0
  52. package/src/lib/combobox/combobox.component.ts +279 -0
  53. package/src/lib/combobox/combobox.variants.ts +19 -0
  54. package/src/lib/combobox/index.ts +2 -0
  55. package/src/lib/diff/diff.component.spec.ts +47 -0
  56. package/src/lib/diff/diff.component.ts +83 -0
  57. package/src/lib/diff/index.ts +1 -0
  58. package/src/lib/divider/divider.component.spec.ts +48 -0
  59. package/src/lib/divider/divider.component.ts +52 -0
  60. package/src/lib/divider/divider.variants.ts +22 -0
  61. package/src/lib/divider/index.ts +2 -0
  62. package/src/lib/dock/dock.directives.spec.ts +85 -0
  63. package/src/lib/dock/dock.directives.ts +83 -0
  64. package/src/lib/dock/index.ts +1 -0
  65. package/src/lib/drawer/drawer.directives.spec.ts +62 -0
  66. package/src/lib/drawer/drawer.directives.ts +83 -0
  67. package/src/lib/drawer/index.ts +8 -0
  68. package/src/lib/dropdown/dropdown.directives.spec.ts +106 -0
  69. package/src/lib/dropdown/dropdown.directives.ts +143 -0
  70. package/src/lib/dropdown/dropdown.variants.ts +27 -0
  71. package/src/lib/dropdown/index.ts +15 -0
  72. package/src/lib/fab/fab.directives.spec.ts +60 -0
  73. package/src/lib/fab/fab.directives.ts +80 -0
  74. package/src/lib/fab/index.ts +8 -0
  75. package/src/lib/fieldset/fieldset.directives.spec.ts +74 -0
  76. package/src/lib/fieldset/fieldset.directives.ts +52 -0
  77. package/src/lib/fieldset/fieldset.variants.ts +15 -0
  78. package/src/lib/fieldset/index.ts +6 -0
  79. package/src/lib/file-input/file-input.component.spec.ts +114 -0
  80. package/src/lib/file-input/file-input.component.ts +168 -0
  81. package/src/lib/file-input/file-input.variants.ts +25 -0
  82. package/src/lib/file-input/index.ts +6 -0
  83. package/src/lib/indicator/index.ts +6 -0
  84. package/src/lib/indicator/indicator.directives.spec.ts +64 -0
  85. package/src/lib/indicator/indicator.directives.ts +61 -0
  86. package/src/lib/input/index.ts +3 -0
  87. package/src/lib/input/input.directive.spec.ts +103 -0
  88. package/src/lib/input/input.directive.ts +26 -0
  89. package/src/lib/input/input.variants.ts +42 -0
  90. package/src/lib/input/label.directive.ts +17 -0
  91. package/src/lib/kbd/index.ts +2 -0
  92. package/src/lib/kbd/kbd.directive.spec.ts +42 -0
  93. package/src/lib/kbd/kbd.directive.ts +19 -0
  94. package/src/lib/kbd/kbd.variants.ts +19 -0
  95. package/src/lib/link/index.ts +2 -0
  96. package/src/lib/link/link.directive.spec.ts +41 -0
  97. package/src/lib/link/link.directive.ts +19 -0
  98. package/src/lib/link/link.variants.ts +20 -0
  99. package/src/lib/list/index.ts +8 -0
  100. package/src/lib/list/list.directives.spec.ts +65 -0
  101. package/src/lib/list/list.directives.ts +86 -0
  102. package/src/lib/loader/index.ts +2 -0
  103. package/src/lib/loader/loader.component.spec.ts +58 -0
  104. package/src/lib/loader/loader.component.ts +48 -0
  105. package/src/lib/loader/loader.variants.ts +21 -0
  106. package/src/lib/modal/dialog-ref.ts +19 -0
  107. package/src/lib/modal/dialog.directives.ts +90 -0
  108. package/src/lib/modal/dialog.service.spec.ts +52 -0
  109. package/src/lib/modal/dialog.service.ts +61 -0
  110. package/src/lib/modal/dialog.types.ts +16 -0
  111. package/src/lib/modal/index.ts +11 -0
  112. package/src/lib/navbar/index.ts +7 -0
  113. package/src/lib/navbar/navbar.directives.spec.ts +59 -0
  114. package/src/lib/navbar/navbar.directives.ts +61 -0
  115. package/src/lib/pagination/index.ts +6 -0
  116. package/src/lib/pagination/pagination.component.spec.ts +59 -0
  117. package/src/lib/pagination/pagination.component.ts +144 -0
  118. package/src/lib/pagination/pagination.variants.ts +31 -0
  119. package/src/lib/progress/index.ts +7 -0
  120. package/src/lib/progress/progress.component.spec.ts +117 -0
  121. package/src/lib/progress/progress.component.ts +65 -0
  122. package/src/lib/progress/progress.variants.ts +43 -0
  123. package/src/lib/radial-progress/index.ts +5 -0
  124. package/src/lib/radial-progress/radial-progress.component.spec.ts +41 -0
  125. package/src/lib/radial-progress/radial-progress.component.ts +71 -0
  126. package/src/lib/radio/index.ts +2 -0
  127. package/src/lib/radio/radio.directive.spec.ts +46 -0
  128. package/src/lib/radio/radio.directive.ts +17 -0
  129. package/src/lib/radio/radio.variants.ts +19 -0
  130. package/src/lib/rating/index.ts +2 -0
  131. package/src/lib/rating/rating.component.spec.ts +157 -0
  132. package/src/lib/rating/rating.component.ts +171 -0
  133. package/src/lib/rating/rating.variants.ts +20 -0
  134. package/src/lib/select/index.ts +2 -0
  135. package/src/lib/select/select.component.spec.ts +112 -0
  136. package/src/lib/select/select.component.ts +250 -0
  137. package/src/lib/select/select.variants.ts +19 -0
  138. package/src/lib/sheet/index.ts +10 -0
  139. package/src/lib/sheet/sheet-ref.ts +18 -0
  140. package/src/lib/sheet/sheet.component.spec.ts +67 -0
  141. package/src/lib/sheet/sheet.directives.ts +75 -0
  142. package/src/lib/sheet/sheet.service.ts +100 -0
  143. package/src/lib/sheet/sheet.types.ts +23 -0
  144. package/src/lib/skeleton/index.ts +2 -0
  145. package/src/lib/skeleton/skeleton.directive.spec.ts +63 -0
  146. package/src/lib/skeleton/skeleton.directive.ts +22 -0
  147. package/src/lib/skeleton/skeleton.variants.ts +27 -0
  148. package/src/lib/slider/index.ts +2 -0
  149. package/src/lib/slider/slider.component.spec.ts +104 -0
  150. package/src/lib/slider/slider.component.ts +188 -0
  151. package/src/lib/slider/slider.variants.ts +25 -0
  152. package/src/lib/stat/index.ts +8 -0
  153. package/src/lib/stat/stat.directives.spec.ts +60 -0
  154. package/src/lib/stat/stat.directives.ts +84 -0
  155. package/src/lib/status/index.ts +2 -0
  156. package/src/lib/status/status.directive.spec.ts +43 -0
  157. package/src/lib/status/status.directive.ts +38 -0
  158. package/src/lib/status/status.variants.ts +26 -0
  159. package/src/lib/steps/index.ts +8 -0
  160. package/src/lib/steps/steps.directives.spec.ts +52 -0
  161. package/src/lib/steps/steps.directives.ts +80 -0
  162. package/src/lib/switch/index.ts +2 -0
  163. package/src/lib/switch/switch.component.spec.ts +98 -0
  164. package/src/lib/switch/switch.component.ts +84 -0
  165. package/src/lib/switch/switch.variants.ts +31 -0
  166. package/src/lib/table/index.ts +12 -0
  167. package/src/lib/table/table.directives.spec.ts +111 -0
  168. package/src/lib/table/table.directives.ts +134 -0
  169. package/src/lib/table/table.variants.ts +36 -0
  170. package/src/lib/tabs/index.ts +8 -0
  171. package/src/lib/tabs/tabs.directives.spec.ts +136 -0
  172. package/src/lib/tabs/tabs.directives.ts +130 -0
  173. package/src/lib/tabs/tabs.variants.ts +17 -0
  174. package/src/lib/textarea/index.ts +7 -0
  175. package/src/lib/textarea/textarea.directive.spec.ts +84 -0
  176. package/src/lib/textarea/textarea.directive.ts +72 -0
  177. package/src/lib/textarea/textarea.variants.ts +34 -0
  178. package/src/lib/timeline/index.ts +11 -0
  179. package/src/lib/timeline/timeline.directives.spec.ts +55 -0
  180. package/src/lib/timeline/timeline.directives.ts +90 -0
  181. package/src/lib/toast/index.ts +3 -0
  182. package/src/lib/toast/toast.service.spec.ts +71 -0
  183. package/src/lib/toast/toast.service.ts +60 -0
  184. package/src/lib/toast/toast.variants.ts +38 -0
  185. package/src/lib/toast/toaster.component.spec.ts +38 -0
  186. package/src/lib/toast/toaster.component.ts +82 -0
  187. package/src/lib/toggle/index.ts +2 -0
  188. package/src/lib/toggle/toggle.directive.spec.ts +100 -0
  189. package/src/lib/toggle/toggle.directive.ts +73 -0
  190. package/src/lib/toggle/toggle.variants.ts +25 -0
  191. package/src/lib/tooltip/index.ts +2 -0
  192. package/src/lib/tooltip/tooltip.directive.spec.ts +113 -0
  193. package/src/lib/tooltip/tooltip.directive.ts +131 -0
  194. package/src/lib/tooltip/tooltip.variants.ts +20 -0
  195. package/src/lib/validator/index.ts +5 -0
  196. package/src/lib/validator/validator.directives.spec.ts +47 -0
  197. package/src/lib/validator/validator.directives.ts +52 -0
  198. package/types/sonny-ui-core.d.ts +878 -11
@@ -0,0 +1,171 @@
1
+ import { ChangeDetectionStrategy, Component, computed, effect, forwardRef, input, model, signal } from '@angular/core';
2
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
3
+ import { cn } from '../core/utils/cn';
4
+ import { ratingVariants, type RatingSize, type RatingVariant } from './rating.variants';
5
+
6
+ interface StarState {
7
+ index: number;
8
+ fill: 'full' | 'half' | 'empty';
9
+ }
10
+
11
+ @Component({
12
+ selector: 'sny-rating',
13
+ standalone: true,
14
+ changeDetection: ChangeDetectionStrategy.OnPush,
15
+ host: {
16
+ 'role': 'slider',
17
+ 'tabindex': '0',
18
+ '[attr.aria-valuenow]': 'value()',
19
+ '[attr.aria-valuemin]': '0',
20
+ '[attr.aria-valuemax]': 'max()',
21
+ '[attr.aria-label]': '"Rating"',
22
+ '[attr.aria-readonly]': 'readonly() || null',
23
+ '[class]': 'computedClass()',
24
+ '(keydown)': 'onKeydown($event)',
25
+ '(mouseleave)': 'onMouseLeave()',
26
+ '(blur)': 'onTouched()',
27
+ },
28
+ providers: [
29
+ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnyRatingComponent), multi: true },
30
+ ],
31
+ template: `
32
+ @for (star of stars(); track star.index) {
33
+ <svg
34
+ xmlns="http://www.w3.org/2000/svg"
35
+ viewBox="0 0 24 24"
36
+ [class]="starClass(star)"
37
+ (click)="onStarClick(star.index + 1)"
38
+ (mouseenter)="onStarHover(star.index + 1)"
39
+ >
40
+ @if (ratingVariant() === 'star') {
41
+ <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
42
+ } @else {
43
+ <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" />
44
+ }
45
+ </svg>
46
+ }
47
+ `,
48
+ })
49
+ export class SnyRatingComponent implements ControlValueAccessor {
50
+ readonly value = model(0);
51
+ readonly max = input(5);
52
+ readonly readonly = input(false);
53
+ readonly size = input<RatingSize>('md');
54
+ readonly ratingVariant = input<RatingVariant>('star');
55
+ readonly half = input(false);
56
+ readonly class = input<string>('');
57
+
58
+ private readonly _disabledByCva = signal(false);
59
+ protected readonly isDisabled = computed(() => this.readonly() || this._disabledByCva());
60
+
61
+ readonly hoverValue = signal<number | null>(null);
62
+
63
+ private _onChange: (value: number) => void = () => {};
64
+ protected onTouched: () => void = () => {};
65
+ private _writing = false;
66
+
67
+ constructor() {
68
+ effect(() => {
69
+ const val = this.value();
70
+ if (this._writing) {
71
+ this._writing = false;
72
+ return;
73
+ }
74
+ this._onChange(val);
75
+ });
76
+ }
77
+
78
+ writeValue(val: number): void {
79
+ this._writing = true;
80
+ this.value.set(val ?? 0);
81
+ }
82
+
83
+ registerOnChange(fn: (value: number) => void): void {
84
+ this._onChange = fn;
85
+ }
86
+
87
+ registerOnTouched(fn: () => void): void {
88
+ this.onTouched = fn;
89
+ }
90
+
91
+ setDisabledState(isDisabled: boolean): void {
92
+ this._disabledByCva.set(isDisabled);
93
+ }
94
+
95
+ readonly displayValue = computed(() => this.hoverValue() ?? this.value());
96
+
97
+ readonly stars = computed<StarState[]>(() => {
98
+ const display = this.displayValue();
99
+ const m = this.max();
100
+ return Array.from({ length: m }, (_, i) => {
101
+ let fill: 'full' | 'half' | 'empty';
102
+ if (i + 1 <= display) {
103
+ fill = 'full';
104
+ } else if (this.half() && i + 0.5 <= display) {
105
+ fill = 'half';
106
+ } else {
107
+ fill = 'empty';
108
+ }
109
+ return { index: i, fill };
110
+ });
111
+ });
112
+
113
+ protected readonly computedClass = computed(() =>
114
+ cn(
115
+ ratingVariants({ size: this.size() }),
116
+ this.isDisabled() && 'pointer-events-none',
117
+ this.class()
118
+ )
119
+ );
120
+
121
+ starClass(star: StarState): string {
122
+ const base = 'cursor-pointer transition-colors';
123
+ switch (star.fill) {
124
+ case 'full':
125
+ return cn(base, 'fill-yellow-400 stroke-yellow-400');
126
+ case 'half':
127
+ return cn(base, 'fill-yellow-400/50 stroke-yellow-400');
128
+ case 'empty':
129
+ return cn(base, 'fill-none stroke-muted-foreground');
130
+ }
131
+ }
132
+
133
+ onStarClick(index: number): void {
134
+ if (this.isDisabled()) return;
135
+ this.value.set(index);
136
+ }
137
+
138
+ onStarHover(index: number): void {
139
+ if (this.isDisabled()) return;
140
+ this.hoverValue.set(index);
141
+ }
142
+
143
+ onMouseLeave(): void {
144
+ this.hoverValue.set(null);
145
+ }
146
+
147
+ onKeydown(event: KeyboardEvent): void {
148
+ if (this.isDisabled()) return;
149
+ const step = this.half() ? 0.5 : 1;
150
+ switch (event.key) {
151
+ case 'ArrowRight':
152
+ case 'ArrowUp':
153
+ event.preventDefault();
154
+ this.value.update((v) => Math.min(this.max(), v + step));
155
+ break;
156
+ case 'ArrowLeft':
157
+ case 'ArrowDown':
158
+ event.preventDefault();
159
+ this.value.update((v) => Math.max(0, v - step));
160
+ break;
161
+ case 'Home':
162
+ event.preventDefault();
163
+ this.value.set(0);
164
+ break;
165
+ case 'End':
166
+ event.preventDefault();
167
+ this.value.set(this.max());
168
+ break;
169
+ }
170
+ }
171
+ }
@@ -0,0 +1,20 @@
1
+ import { cva } from 'class-variance-authority';
2
+
3
+ export const ratingVariants = cva(
4
+ 'inline-flex items-center gap-0.5 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 rounded',
5
+ {
6
+ variants: {
7
+ size: {
8
+ sm: '[&_svg]:w-4 [&_svg]:h-4',
9
+ md: '[&_svg]:w-5 [&_svg]:h-5',
10
+ lg: '[&_svg]:w-7 [&_svg]:h-7',
11
+ },
12
+ },
13
+ defaultVariants: {
14
+ size: 'md',
15
+ },
16
+ }
17
+ );
18
+
19
+ export type RatingSize = 'sm' | 'md' | 'lg';
20
+ export type RatingVariant = 'star' | 'heart';
@@ -0,0 +1,2 @@
1
+ export { SnySelectComponent, type SelectOption } from './select.component';
2
+ export { selectTriggerVariants, type SelectSize } from './select.variants';
@@ -0,0 +1,112 @@
1
+ import { Component, signal } from '@angular/core';
2
+ import { FormControl, ReactiveFormsModule } from '@angular/forms';
3
+ import { TestBed, ComponentFixture } from '@angular/core/testing';
4
+ import { SnySelectComponent, type SelectOption } from './select.component';
5
+
6
+ @Component({
7
+ standalone: true,
8
+ imports: [SnySelectComponent],
9
+ template: `<sny-select [options]="options" [(value)]="value" [placeholder]="placeholder()" />`,
10
+ })
11
+ class TestHostComponent {
12
+ options: SelectOption[] = [
13
+ { value: 'a', label: 'Option A' },
14
+ { value: 'b', label: 'Option B' },
15
+ { value: 'c', label: 'Option C' },
16
+ ];
17
+ value = signal('');
18
+ placeholder = signal('Pick one...');
19
+ }
20
+
21
+ describe('SnySelectComponent', () => {
22
+ let fixture: ComponentFixture<TestHostComponent>;
23
+ let trigger: HTMLButtonElement;
24
+
25
+ beforeEach(async () => {
26
+ await TestBed.configureTestingModule({
27
+ imports: [TestHostComponent],
28
+ }).compileComponents();
29
+
30
+ fixture = TestBed.createComponent(TestHostComponent);
31
+ fixture.detectChanges();
32
+ trigger = fixture.nativeElement.querySelector('button');
33
+ });
34
+
35
+ it('should show placeholder when no value', () => {
36
+ expect(trigger.textContent).toContain('Pick one...');
37
+ });
38
+
39
+ it('should have combobox role', () => {
40
+ expect(trigger.getAttribute('role')).toBe('combobox');
41
+ expect(trigger.getAttribute('aria-expanded')).toBe('false');
42
+ });
43
+
44
+ it('should open dropdown on click', () => {
45
+ trigger.click();
46
+ fixture.detectChanges();
47
+ expect(trigger.getAttribute('aria-expanded')).toBe('true');
48
+ const options = fixture.nativeElement.querySelectorAll('[role="option"]');
49
+ expect(options.length).toBe(3);
50
+ });
51
+
52
+ it('should show selected label', () => {
53
+ fixture.componentInstance.value.set('b');
54
+ fixture.detectChanges();
55
+ expect(trigger.textContent).toContain('Option B');
56
+ });
57
+ });
58
+
59
+ @Component({
60
+ standalone: true,
61
+ imports: [ReactiveFormsModule, SnySelectComponent],
62
+ template: `<sny-select [options]="options" [formControl]="ctrl" />`,
63
+ })
64
+ class ReactiveFormHost {
65
+ options: SelectOption[] = [
66
+ { value: 'a', label: 'Option A' },
67
+ { value: 'b', label: 'Option B' },
68
+ { value: 'c', label: 'Option C' },
69
+ ];
70
+ ctrl = new FormControl('');
71
+ }
72
+
73
+ describe('SnySelectComponent — Reactive Forms', () => {
74
+ let fixture: ComponentFixture<ReactiveFormHost>;
75
+ let trigger: HTMLButtonElement;
76
+
77
+ beforeEach(async () => {
78
+ await TestBed.configureTestingModule({
79
+ imports: [ReactiveFormHost],
80
+ }).compileComponents();
81
+ fixture = TestBed.createComponent(ReactiveFormHost);
82
+ fixture.detectChanges();
83
+ trigger = fixture.nativeElement.querySelector('button');
84
+ });
85
+
86
+ it('should update view when FormControl value changes (writeValue)', () => {
87
+ fixture.componentInstance.ctrl.setValue('b');
88
+ fixture.detectChanges();
89
+ expect(trigger.textContent).toContain('Option B');
90
+ });
91
+
92
+ it('should update FormControl when user interacts (onChange)', () => {
93
+ trigger.click();
94
+ fixture.detectChanges();
95
+ const option = fixture.nativeElement.querySelector('[role="option"]') as HTMLElement;
96
+ option.dispatchEvent(new Event('mousedown'));
97
+ fixture.detectChanges();
98
+ expect(fixture.componentInstance.ctrl.value).toBe('a');
99
+ });
100
+
101
+ it('should disable via FormControl.disable() (setDisabledState)', () => {
102
+ fixture.componentInstance.ctrl.disable();
103
+ fixture.detectChanges();
104
+ expect(trigger.disabled).toBe(true);
105
+ });
106
+
107
+ it('should mark as touched on blur (onTouched)', () => {
108
+ expect(fixture.componentInstance.ctrl.touched).toBe(false);
109
+ trigger.dispatchEvent(new Event('blur'));
110
+ expect(fixture.componentInstance.ctrl.touched).toBe(true);
111
+ });
112
+ });
@@ -0,0 +1,250 @@
1
+ import {
2
+ ChangeDetectionStrategy,
3
+ Component,
4
+ computed,
5
+ effect,
6
+ ElementRef,
7
+ forwardRef,
8
+ HostListener,
9
+ inject,
10
+ input,
11
+ model,
12
+ OnDestroy,
13
+ signal,
14
+ viewChild,
15
+ } from '@angular/core';
16
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
17
+ import { cn } from '../core/utils/cn';
18
+ import { selectTriggerVariants, type SelectSize } from './select.variants';
19
+
20
+ export interface SelectOption {
21
+ value: string;
22
+ label: string;
23
+ }
24
+
25
+ @Component({
26
+ selector: 'sny-select',
27
+ standalone: true,
28
+ changeDetection: ChangeDetectionStrategy.OnPush,
29
+ host: {
30
+ class: 'relative inline-block w-full',
31
+ },
32
+ providers: [
33
+ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnySelectComponent), multi: true },
34
+ ],
35
+ template: `
36
+ <button
37
+ #triggerEl
38
+ type="button"
39
+ role="combobox"
40
+ [attr.aria-expanded]="open()"
41
+ aria-haspopup="listbox"
42
+ [disabled]="isDisabled()"
43
+ [class]="triggerClass()"
44
+ (click)="toggle()"
45
+ (keydown)="onTriggerKeydown($event)"
46
+ (blur)="onTouched()"
47
+ >
48
+ <span [class]="selectedLabel() ? '' : 'text-muted-foreground'">
49
+ {{ selectedLabel() || placeholder() }}
50
+ </span>
51
+ <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="shrink-0 opacity-50"><path d="m6 9 6 6 6-6"/></svg>
52
+ </button>
53
+
54
+ @if (open()) {
55
+ <div
56
+ #dropdownEl
57
+ class="fixed z-50 rounded-sm border border-border bg-popover text-popover-foreground shadow-md"
58
+ >
59
+ <ul role="listbox" class="max-h-60 overflow-auto p-1">
60
+ @for (opt of options(); track opt.value; let i = $index) {
61
+ <li
62
+ role="option"
63
+ [attr.aria-selected]="value() === opt.value"
64
+ [class]="optionClass(i)"
65
+ (mousedown)="select(opt); $event.preventDefault()"
66
+ (mouseenter)="activeIndex.set(i)"
67
+ >
68
+ <svg
69
+ xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"
70
+ fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
71
+ [class]="value() === opt.value ? 'mr-2 shrink-0 opacity-100' : 'mr-2 shrink-0 opacity-0'"
72
+ ><path d="M20 6 9 17l-5-5"/></svg>
73
+ {{ opt.label }}
74
+ </li>
75
+ }
76
+ </ul>
77
+ </div>
78
+ }
79
+ `,
80
+ })
81
+ export class SnySelectComponent implements ControlValueAccessor, OnDestroy {
82
+ readonly options = input<SelectOption[]>([]);
83
+ readonly placeholder = input('Select...');
84
+ readonly size = input<SelectSize>('md');
85
+ readonly disabled = input(false);
86
+ readonly class = input<string>('');
87
+ readonly value = model<string>('');
88
+
89
+ readonly open = signal(false);
90
+ readonly activeIndex = signal(0);
91
+
92
+ private readonly _disabledByCva = signal(false);
93
+ protected readonly isDisabled = computed(() => this.disabled() || this._disabledByCva());
94
+
95
+ private readonly triggerRef = viewChild<ElementRef<HTMLButtonElement>>('triggerEl');
96
+ private readonly dropdownRef = viewChild<ElementRef<HTMLDivElement>>('dropdownEl');
97
+ private readonly elRef = inject(ElementRef);
98
+
99
+ private scrollHandler: (() => void) | null = null;
100
+ private resizeHandler: (() => void) | null = null;
101
+
102
+ private _onChange: (value: string) => void = () => {};
103
+ protected onTouched: () => void = () => {};
104
+ private _writing = false;
105
+
106
+ constructor() {
107
+ effect(() => {
108
+ const val = this.value();
109
+ if (this._writing) {
110
+ this._writing = false;
111
+ return;
112
+ }
113
+ this._onChange(val);
114
+ });
115
+ }
116
+
117
+ writeValue(val: string): void {
118
+ this._writing = true;
119
+ this.value.set(val ?? '');
120
+ }
121
+
122
+ registerOnChange(fn: (value: string) => void): void {
123
+ this._onChange = fn;
124
+ }
125
+
126
+ registerOnTouched(fn: () => void): void {
127
+ this.onTouched = fn;
128
+ }
129
+
130
+ setDisabledState(isDisabled: boolean): void {
131
+ this._disabledByCva.set(isDisabled);
132
+ }
133
+
134
+ readonly selectedLabel = computed(() => {
135
+ const v = this.value();
136
+ if (!v) return '';
137
+ const opt = this.options().find(o => o.value === v);
138
+ return opt?.label ?? '';
139
+ });
140
+
141
+ protected readonly triggerClass = computed(() =>
142
+ cn(selectTriggerVariants({ size: this.size() }), this.class())
143
+ );
144
+
145
+ optionClass(index: number): string {
146
+ const base = 'relative flex cursor-pointer select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors';
147
+ const active = index === this.activeIndex() ? 'bg-accent text-accent-foreground' : 'hover:bg-accent/50';
148
+ return cn(base, active);
149
+ }
150
+
151
+ private updateDropdownPosition(): void {
152
+ const trigger = this.triggerRef()?.nativeElement;
153
+ if (!trigger) return;
154
+ const rect = trigger.getBoundingClientRect();
155
+ const dropdown = this.dropdownRef()?.nativeElement;
156
+ if (dropdown) {
157
+ dropdown.style.top = `${rect.bottom + 4}px`;
158
+ dropdown.style.left = `${rect.left}px`;
159
+ dropdown.style.width = `${rect.width}px`;
160
+ }
161
+ }
162
+
163
+ private addGlobalListeners(): void {
164
+ this.removeGlobalListeners();
165
+ this.scrollHandler = () => {
166
+ requestAnimationFrame(() => this.updateDropdownPosition());
167
+ };
168
+ this.resizeHandler = () => {
169
+ requestAnimationFrame(() => this.updateDropdownPosition());
170
+ };
171
+ document.addEventListener('scroll', this.scrollHandler, { capture: true, passive: true });
172
+ window.addEventListener('resize', this.resizeHandler, { passive: true });
173
+ }
174
+
175
+ private removeGlobalListeners(): void {
176
+ if (this.scrollHandler) {
177
+ document.removeEventListener('scroll', this.scrollHandler, { capture: true } as EventListenerOptions);
178
+ this.scrollHandler = null;
179
+ }
180
+ if (this.resizeHandler) {
181
+ window.removeEventListener('resize', this.resizeHandler);
182
+ this.resizeHandler = null;
183
+ }
184
+ }
185
+
186
+ ngOnDestroy(): void {
187
+ this.removeGlobalListeners();
188
+ }
189
+
190
+ toggle(): void {
191
+ if (this.open()) {
192
+ this.close();
193
+ } else {
194
+ this.open.set(true);
195
+ this.activeIndex.set(
196
+ Math.max(0, this.options().findIndex(o => o.value === this.value()))
197
+ );
198
+ this.addGlobalListeners();
199
+ setTimeout(() => this.updateDropdownPosition());
200
+ }
201
+ }
202
+
203
+ close(): void {
204
+ this.open.set(false);
205
+ this.removeGlobalListeners();
206
+ }
207
+
208
+ select(opt: SelectOption): void {
209
+ this.value.set(opt.value);
210
+ this.close();
211
+ }
212
+
213
+ onTriggerKeydown(event: KeyboardEvent): void {
214
+ const items = this.options();
215
+ if (!this.open()) {
216
+ if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') {
217
+ event.preventDefault();
218
+ this.toggle();
219
+ }
220
+ return;
221
+ }
222
+ switch (event.key) {
223
+ case 'ArrowDown':
224
+ event.preventDefault();
225
+ this.activeIndex.update(i => Math.min(i + 1, items.length - 1));
226
+ break;
227
+ case 'ArrowUp':
228
+ event.preventDefault();
229
+ this.activeIndex.update(i => Math.max(i - 1, 0));
230
+ break;
231
+ case 'Enter':
232
+ case ' ':
233
+ event.preventDefault();
234
+ if (items[this.activeIndex()]) {
235
+ this.select(items[this.activeIndex()]);
236
+ }
237
+ break;
238
+ case 'Escape':
239
+ this.close();
240
+ break;
241
+ }
242
+ }
243
+
244
+ @HostListener('document:click', ['$event'])
245
+ onDocumentClick(event: MouseEvent): void {
246
+ if (!this.elRef.nativeElement.contains(event.target)) {
247
+ this.close();
248
+ }
249
+ }
250
+ }
@@ -0,0 +1,19 @@
1
+ import { cva } from 'class-variance-authority';
2
+
3
+ export const selectTriggerVariants = cva(
4
+ 'inline-flex w-full items-center justify-between whitespace-nowrap rounded-sm border border-border bg-background px-3 py-2 text-sm ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
5
+ {
6
+ variants: {
7
+ size: {
8
+ sm: 'h-9 text-xs',
9
+ md: 'h-10 text-sm',
10
+ lg: 'h-11 text-base',
11
+ },
12
+ },
13
+ defaultVariants: {
14
+ size: 'md',
15
+ },
16
+ }
17
+ );
18
+
19
+ export type SelectSize = 'sm' | 'md' | 'lg';
@@ -0,0 +1,10 @@
1
+ export { SnySheetService, SNY_SHEET_DATA } from './sheet.service';
2
+ export { SnySheetRef } from './sheet-ref';
3
+ export {
4
+ SnySheetHeaderDirective,
5
+ SnySheetTitleDirective,
6
+ SnySheetDescriptionDirective,
7
+ SnySheetContentDirective,
8
+ SnySheetCloseDirective,
9
+ } from './sheet.directives';
10
+ export { type SnySheetConfig, type SheetSide } from './sheet.types';
@@ -0,0 +1,18 @@
1
+ import type { Observable } from 'rxjs';
2
+
3
+ interface CdkDialogRefLike<R> {
4
+ close(result?: R): void;
5
+ readonly closed: Observable<R | undefined>;
6
+ }
7
+
8
+ export class SnySheetRef<R = unknown> {
9
+ constructor(private readonly cdkRef: CdkDialogRefLike<R>) {}
10
+
11
+ close(result?: R): void {
12
+ this.cdkRef.close(result);
13
+ }
14
+
15
+ get closed(): Observable<R | undefined> {
16
+ return this.cdkRef.closed;
17
+ }
18
+ }
@@ -0,0 +1,67 @@
1
+ import { Component, inject } from '@angular/core';
2
+ import { TestBed } from '@angular/core/testing';
3
+ import { DialogModule, DialogRef } from '@angular/cdk/dialog';
4
+ import { SnySheetService } from './sheet.service';
5
+ import {
6
+ SnySheetHeaderDirective,
7
+ SnySheetTitleDirective,
8
+ SnySheetDescriptionDirective,
9
+ SnySheetCloseDirective,
10
+ } from './sheet.directives';
11
+ import { SnyButtonDirective } from '../button/button.directive';
12
+
13
+ @Component({
14
+ standalone: true,
15
+ imports: [
16
+ SnySheetHeaderDirective,
17
+ SnySheetTitleDirective,
18
+ SnySheetDescriptionDirective,
19
+ SnySheetCloseDirective,
20
+ SnyButtonDirective,
21
+ ],
22
+ template: `
23
+ <div>
24
+ <div snySheetHeader>
25
+ <h2 snySheetTitle>Test Sheet</h2>
26
+ <p snySheetDescription>A test sheet.</p>
27
+ </div>
28
+ <button snySheetClose aria-label="Close">X</button>
29
+ <button snyBtn (click)="dialogRef.close('done')">Done</button>
30
+ </div>
31
+ `,
32
+ })
33
+ class TestSheetComponent {
34
+ readonly dialogRef = inject(DialogRef);
35
+ }
36
+
37
+ describe('SnySheetService', () => {
38
+ let service: SnySheetService;
39
+
40
+ beforeEach(async () => {
41
+ await TestBed.configureTestingModule({
42
+ imports: [DialogModule],
43
+ }).compileComponents();
44
+
45
+ service = TestBed.inject(SnySheetService);
46
+ });
47
+
48
+ it('should be created', () => {
49
+ expect(service).toBeTruthy();
50
+ });
51
+
52
+ it('should open a sheet', () => {
53
+ const ref = service.open(TestSheetComponent, { side: 'right' });
54
+ expect(ref).toBeTruthy();
55
+ ref.close();
56
+ });
57
+
58
+ it('should open from different sides', () => {
59
+ const refLeft = service.open(TestSheetComponent, { side: 'left' });
60
+ expect(refLeft).toBeTruthy();
61
+ refLeft.close();
62
+
63
+ const refTop = service.open(TestSheetComponent, { side: 'top' });
64
+ expect(refTop).toBeTruthy();
65
+ refTop.close();
66
+ });
67
+ });