@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,75 @@
1
+ import { Directive, computed, input, inject } from '@angular/core';
2
+ import { DialogRef } from '@angular/cdk/dialog';
3
+ import { cn } from '../core/utils/cn';
4
+
5
+ @Directive({
6
+ selector: '[snySheetHeader]',
7
+ standalone: true,
8
+ host: { '[class]': 'computedClass()' },
9
+ })
10
+ export class SnySheetHeaderDirective {
11
+ readonly class = input<string>('');
12
+ protected readonly computedClass = computed(() =>
13
+ cn('flex flex-col space-y-2', this.class())
14
+ );
15
+ }
16
+
17
+ @Directive({
18
+ selector: '[snySheetTitle]',
19
+ standalone: true,
20
+ host: { '[class]': 'computedClass()' },
21
+ })
22
+ export class SnySheetTitleDirective {
23
+ readonly class = input<string>('');
24
+ protected readonly computedClass = computed(() =>
25
+ cn('text-lg font-semibold text-foreground', this.class())
26
+ );
27
+ }
28
+
29
+ @Directive({
30
+ selector: '[snySheetDescription]',
31
+ standalone: true,
32
+ host: { '[class]': 'computedClass()' },
33
+ })
34
+ export class SnySheetDescriptionDirective {
35
+ readonly class = input<string>('');
36
+ protected readonly computedClass = computed(() =>
37
+ cn('text-sm text-muted-foreground', this.class())
38
+ );
39
+ }
40
+
41
+ @Directive({
42
+ selector: '[snySheetContent]',
43
+ standalone: true,
44
+ host: { '[class]': 'computedClass()' },
45
+ })
46
+ export class SnySheetContentDirective {
47
+ readonly class = input<string>('');
48
+ protected readonly computedClass = computed(() =>
49
+ cn('py-4', this.class())
50
+ );
51
+ }
52
+
53
+ @Directive({
54
+ selector: '[snySheetClose]',
55
+ standalone: true,
56
+ host: {
57
+ '[class]': 'computedClass()',
58
+ '(click)': 'onClick()',
59
+ },
60
+ })
61
+ export class SnySheetCloseDirective {
62
+ readonly class = input<string>('');
63
+ protected readonly computedClass = computed(() =>
64
+ cn(
65
+ 'absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
66
+ this.class()
67
+ )
68
+ );
69
+
70
+ private readonly dialogRef = inject(DialogRef, { optional: true });
71
+
72
+ onClick(): void {
73
+ this.dialogRef?.close();
74
+ }
75
+ }
@@ -0,0 +1,100 @@
1
+ import { Injectable, inject, InjectionToken, Injector } from '@angular/core';
2
+ import { Dialog, DialogRef as CdkDialogRef } from '@angular/cdk/dialog';
3
+ import { type ComponentType, createGlobalPositionStrategy, type GlobalPositionStrategy } from '@angular/cdk/overlay';
4
+ import { SnySheetRef } from './sheet-ref';
5
+ import { DEFAULT_SHEET_CONFIG, SHEET_PANEL_CLASS, type SheetSide, type SnySheetConfig } from './sheet.types';
6
+
7
+ export const SNY_SHEET_DATA = new InjectionToken<unknown>('SNY_SHEET_DATA');
8
+
9
+ interface SheetOverlayConfig {
10
+ positionStrategy: GlobalPositionStrategy;
11
+ width?: string;
12
+ maxWidth?: string;
13
+ height?: string;
14
+ maxHeight?: string;
15
+ }
16
+
17
+ @Injectable({ providedIn: 'root' })
18
+ export class SnySheetService {
19
+ private readonly cdkDialog = inject(Dialog);
20
+ private readonly injector = inject(Injector);
21
+
22
+ open<T, R = unknown>(component: ComponentType<T>, config: SnySheetConfig = {}): SnySheetRef<R> {
23
+ const merged = { ...DEFAULT_SHEET_CONFIG, ...config };
24
+ const side = merged.side ?? 'right';
25
+ const disableClose = !merged.closeOnBackdrop || !merged.closeOnEsc;
26
+ const overlay = this._getOverlayConfig(side);
27
+
28
+ const cdkRef: CdkDialogRef<R, T> = this.cdkDialog.open(component, {
29
+ disableClose,
30
+ hasBackdrop: true,
31
+ backdropClass: 'sny-dialog-backdrop',
32
+ panelClass: ['sny-sheet-panel', SHEET_PANEL_CLASS[side]],
33
+ positionStrategy: overlay.positionStrategy,
34
+ width: overlay.width,
35
+ maxWidth: overlay.maxWidth,
36
+ height: overlay.height,
37
+ maxHeight: overlay.maxHeight,
38
+ ariaLabelledBy: merged.ariaLabelledBy,
39
+ ariaDescribedBy: merged.ariaDescribedBy,
40
+ data: merged.data,
41
+ providers: merged.data != null
42
+ ? [{ provide: SNY_SHEET_DATA, useValue: merged.data }]
43
+ : [],
44
+ });
45
+
46
+ if (disableClose) {
47
+ if (merged.closeOnBackdrop) {
48
+ const sub = cdkRef.backdropClick.subscribe(() => cdkRef.close());
49
+ cdkRef.closed.subscribe(() => sub.unsubscribe());
50
+ }
51
+ if (merged.closeOnEsc) {
52
+ const sub = cdkRef.keydownEvents.subscribe(event => {
53
+ if (event.key === 'Escape') cdkRef.close();
54
+ });
55
+ cdkRef.closed.subscribe(() => sub.unsubscribe());
56
+ }
57
+ }
58
+
59
+ return new SnySheetRef<R>(cdkRef);
60
+ }
61
+
62
+ closeAll(): void {
63
+ this.cdkDialog.closeAll();
64
+ }
65
+
66
+ private _getOverlayConfig(side: SheetSide): SheetOverlayConfig {
67
+ const strategy = createGlobalPositionStrategy(this.injector);
68
+
69
+ switch (side) {
70
+ case 'right':
71
+ return {
72
+ positionStrategy: strategy.top('0').right('0'),
73
+ width: '75%',
74
+ maxWidth: '24rem',
75
+ height: '100vh',
76
+ maxHeight: '100vh',
77
+ };
78
+ case 'left':
79
+ return {
80
+ positionStrategy: strategy.top('0').left('0'),
81
+ width: '75%',
82
+ maxWidth: '24rem',
83
+ height: '100vh',
84
+ maxHeight: '100vh',
85
+ };
86
+ case 'top':
87
+ return {
88
+ positionStrategy: strategy.top('0').centerHorizontally(),
89
+ width: '100vw',
90
+ maxWidth: '100vw',
91
+ };
92
+ case 'bottom':
93
+ return {
94
+ positionStrategy: strategy.bottom('0').centerHorizontally(),
95
+ width: '100vw',
96
+ maxWidth: '100vw',
97
+ };
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,23 @@
1
+ export type SheetSide = 'left' | 'right' | 'top' | 'bottom';
2
+
3
+ export interface SnySheetConfig {
4
+ side?: SheetSide;
5
+ closeOnBackdrop?: boolean;
6
+ closeOnEsc?: boolean;
7
+ data?: unknown;
8
+ ariaLabelledBy?: string;
9
+ ariaDescribedBy?: string;
10
+ }
11
+
12
+ export const DEFAULT_SHEET_CONFIG: SnySheetConfig = {
13
+ side: 'right',
14
+ closeOnBackdrop: true,
15
+ closeOnEsc: true,
16
+ };
17
+
18
+ export const SHEET_PANEL_CLASS: Record<SheetSide, string> = {
19
+ right: 'sny-sheet-right',
20
+ left: 'sny-sheet-left',
21
+ top: 'sny-sheet-top',
22
+ bottom: 'sny-sheet-bottom',
23
+ };
@@ -0,0 +1,2 @@
1
+ export { SnySkeletonDirective } from './skeleton.directive';
2
+ export { skeletonVariants, type SkeletonVariant, type SkeletonSize } from './skeleton.variants';
@@ -0,0 +1,63 @@
1
+ import { Component, signal } from '@angular/core';
2
+ import { TestBed, ComponentFixture } from '@angular/core/testing';
3
+ import { SnySkeletonDirective } from './skeleton.directive';
4
+ import type { SkeletonVariant, SkeletonSize } from './skeleton.variants';
5
+
6
+ @Component({
7
+ standalone: true,
8
+ imports: [SnySkeletonDirective],
9
+ template: `<div snySkeleton [variant]="variant()" [size]="size()"></div>`,
10
+ })
11
+ class TestHostComponent {
12
+ variant = signal<SkeletonVariant>('line');
13
+ size = signal<SkeletonSize>('md');
14
+ }
15
+
16
+ describe('SnySkeletonDirective', () => {
17
+ let fixture: ComponentFixture<TestHostComponent>;
18
+ let el: HTMLElement;
19
+
20
+ beforeEach(async () => {
21
+ await TestBed.configureTestingModule({
22
+ imports: [TestHostComponent],
23
+ }).compileComponents();
24
+
25
+ fixture = TestBed.createComponent(TestHostComponent);
26
+ fixture.detectChanges();
27
+ el = fixture.nativeElement.querySelector('div');
28
+ });
29
+
30
+ it('should apply default classes', () => {
31
+ expect(el.className).toContain('animate-pulse');
32
+ expect(el.className).toContain('bg-muted');
33
+ expect(el.className).toContain('rounded-sm');
34
+ expect(el.className).toContain('h-6');
35
+ });
36
+
37
+ it('should apply circular variant', () => {
38
+ fixture.componentInstance.variant.set('circular');
39
+ fixture.detectChanges();
40
+ expect(el.className).toContain('rounded-full');
41
+ expect(el.className).toContain('aspect-square');
42
+ });
43
+
44
+ it('should apply sm size', () => {
45
+ fixture.componentInstance.size.set('sm');
46
+ fixture.detectChanges();
47
+ expect(el.className).toContain('h-4');
48
+ });
49
+
50
+ it('should apply xl size', () => {
51
+ fixture.componentInstance.size.set('xl');
52
+ fixture.detectChanges();
53
+ expect(el.className).toContain('h-12');
54
+ });
55
+
56
+ it('should have aria-busy="true"', () => {
57
+ expect(el.getAttribute('aria-busy')).toBe('true');
58
+ });
59
+
60
+ it('should have aria-hidden="true"', () => {
61
+ expect(el.getAttribute('aria-hidden')).toBe('true');
62
+ });
63
+ });
@@ -0,0 +1,22 @@
1
+ import { Directive, computed, input } from '@angular/core';
2
+ import { cn } from '../core/utils/cn';
3
+ import { skeletonVariants, type SkeletonVariant, type SkeletonSize } from './skeleton.variants';
4
+
5
+ @Directive({
6
+ selector: '[snySkeleton]',
7
+ standalone: true,
8
+ host: {
9
+ '[class]': 'computedClass()',
10
+ 'aria-busy': 'true',
11
+ 'aria-hidden': 'true',
12
+ },
13
+ })
14
+ export class SnySkeletonDirective {
15
+ readonly variant = input<SkeletonVariant>('line');
16
+ readonly size = input<SkeletonSize>('md');
17
+ readonly class = input<string>('');
18
+
19
+ protected readonly computedClass = computed(() =>
20
+ cn(skeletonVariants({ variant: this.variant(), size: this.size() }), this.class())
21
+ );
22
+ }
@@ -0,0 +1,27 @@
1
+ import { cva } from 'class-variance-authority';
2
+
3
+ export const skeletonVariants = cva(
4
+ 'animate-pulse bg-muted block',
5
+ {
6
+ variants: {
7
+ variant: {
8
+ line: 'rounded-sm',
9
+ circular: 'rounded-full aspect-square',
10
+ rounded: 'rounded-lg',
11
+ },
12
+ size: {
13
+ sm: 'h-4',
14
+ md: 'h-6',
15
+ lg: 'h-8',
16
+ xl: 'h-12',
17
+ },
18
+ },
19
+ defaultVariants: {
20
+ variant: 'line',
21
+ size: 'md',
22
+ },
23
+ }
24
+ );
25
+
26
+ export type SkeletonVariant = 'line' | 'circular' | 'rounded';
27
+ export type SkeletonSize = 'sm' | 'md' | 'lg' | 'xl';
@@ -0,0 +1,2 @@
1
+ export { SnySliderComponent } from './slider.component';
2
+ export { sliderTrackVariants, type SliderSize } from './slider.variants';
@@ -0,0 +1,104 @@
1
+ import { Component, signal } from '@angular/core';
2
+ import { FormControl, ReactiveFormsModule } from '@angular/forms';
3
+ import { TestBed, ComponentFixture } from '@angular/core/testing';
4
+ import { SnySliderComponent } from './slider.component';
5
+
6
+ @Component({
7
+ standalone: true,
8
+ imports: [SnySliderComponent],
9
+ template: `<sny-slider [(value)]="value" [min]="min()" [max]="max()" [step]="step()" [disabled]="disabled()" />`,
10
+ })
11
+ class TestHostComponent {
12
+ value = signal(50);
13
+ min = signal(0);
14
+ max = signal(100);
15
+ step = signal(1);
16
+ disabled = signal(false);
17
+ }
18
+
19
+ describe('SnySliderComponent', () => {
20
+ let fixture: ComponentFixture<TestHostComponent>;
21
+ let thumb: HTMLButtonElement;
22
+
23
+ beforeEach(async () => {
24
+ await TestBed.configureTestingModule({
25
+ imports: [TestHostComponent],
26
+ }).compileComponents();
27
+
28
+ fixture = TestBed.createComponent(TestHostComponent);
29
+ fixture.detectChanges();
30
+ thumb = fixture.nativeElement.querySelector('button[role="slider"]');
31
+ });
32
+
33
+ it('should have slider role', () => {
34
+ expect(thumb.getAttribute('role')).toBe('slider');
35
+ });
36
+
37
+ it('should set aria values', () => {
38
+ expect(thumb.getAttribute('aria-valuemin')).toBe('0');
39
+ expect(thumb.getAttribute('aria-valuemax')).toBe('100');
40
+ expect(thumb.getAttribute('aria-valuenow')).toBe('50');
41
+ });
42
+
43
+ it('should increment with ArrowRight', () => {
44
+ const host = fixture.nativeElement.querySelector('sny-slider');
45
+ host.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' }));
46
+ fixture.detectChanges();
47
+ expect(thumb.getAttribute('aria-valuenow')).toBe('51');
48
+ });
49
+
50
+ it('should decrement with ArrowLeft', () => {
51
+ const host = fixture.nativeElement.querySelector('sny-slider');
52
+ host.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft' }));
53
+ fixture.detectChanges();
54
+ expect(thumb.getAttribute('aria-valuenow')).toBe('49');
55
+ });
56
+ });
57
+
58
+ @Component({
59
+ standalone: true,
60
+ imports: [ReactiveFormsModule, SnySliderComponent],
61
+ template: `<sny-slider [formControl]="ctrl" />`,
62
+ })
63
+ class ReactiveFormHost {
64
+ ctrl = new FormControl(50);
65
+ }
66
+
67
+ describe('SnySliderComponent — Reactive Forms', () => {
68
+ let fixture: ComponentFixture<ReactiveFormHost>;
69
+ let thumb: HTMLButtonElement;
70
+
71
+ beforeEach(async () => {
72
+ await TestBed.configureTestingModule({
73
+ imports: [ReactiveFormHost],
74
+ }).compileComponents();
75
+ fixture = TestBed.createComponent(ReactiveFormHost);
76
+ fixture.detectChanges();
77
+ thumb = fixture.nativeElement.querySelector('button[role="slider"]');
78
+ });
79
+
80
+ it('should update view when FormControl value changes (writeValue)', () => {
81
+ fixture.componentInstance.ctrl.setValue(75);
82
+ fixture.detectChanges();
83
+ expect(thumb.getAttribute('aria-valuenow')).toBe('75');
84
+ });
85
+
86
+ it('should update FormControl when user interacts (onChange)', () => {
87
+ const host = fixture.nativeElement.querySelector('sny-slider');
88
+ host.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' }));
89
+ fixture.detectChanges();
90
+ expect(fixture.componentInstance.ctrl.value).toBe(51);
91
+ });
92
+
93
+ it('should disable via FormControl.disable() (setDisabledState)', () => {
94
+ fixture.componentInstance.ctrl.disable();
95
+ fixture.detectChanges();
96
+ expect(thumb.disabled).toBe(true);
97
+ });
98
+
99
+ it('should mark as touched on blur (onTouched)', () => {
100
+ expect(fixture.componentInstance.ctrl.touched).toBe(false);
101
+ thumb.dispatchEvent(new Event('blur'));
102
+ expect(fixture.componentInstance.ctrl.touched).toBe(true);
103
+ });
104
+ });
@@ -0,0 +1,188 @@
1
+ import { ChangeDetectionStrategy, Component, computed, effect, ElementRef, forwardRef, input, model, OnDestroy, signal, viewChild } from '@angular/core';
2
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
3
+ import { cn } from '../core/utils/cn';
4
+ import { sliderTrackVariants, sliderThumbSize, type SliderSize } from './slider.variants';
5
+
6
+ @Component({
7
+ selector: 'sny-slider',
8
+ standalone: true,
9
+ changeDetection: ChangeDetectionStrategy.OnPush,
10
+ host: {
11
+ class: 'block',
12
+ '(keydown)': 'onKeydown($event)',
13
+ },
14
+ providers: [
15
+ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnySliderComponent), multi: true },
16
+ ],
17
+ template: `
18
+ <div
19
+ #trackEl
20
+ [class]="trackClass()"
21
+ (mousedown)="onTrackMousedown($event)"
22
+ (touchstart)="onTrackTouchstart($event)"
23
+ >
24
+ <div class="absolute h-full rounded-full bg-primary" [style.width.%]="percentage()"></div>
25
+ <button
26
+ type="button"
27
+ role="slider"
28
+ [attr.aria-valuemin]="min()"
29
+ [attr.aria-valuemax]="max()"
30
+ [attr.aria-valuenow]="value()"
31
+ [disabled]="isDisabled()"
32
+ [class]="thumbClass()"
33
+ [style.left.%]="percentage()"
34
+ tabindex="0"
35
+ (blur)="onTouched()"
36
+ ></button>
37
+ </div>
38
+ `,
39
+ })
40
+ export class SnySliderComponent implements ControlValueAccessor, OnDestroy {
41
+ readonly value = model(0);
42
+ readonly min = input(0);
43
+ readonly max = input(100);
44
+ readonly step = input(1);
45
+ readonly disabled = input(false);
46
+ readonly size = input<SliderSize>('md');
47
+ readonly class = input<string>('');
48
+
49
+ private readonly _disabledByCva = signal(false);
50
+ protected readonly isDisabled = computed(() => this.disabled() || this._disabledByCva());
51
+
52
+ private readonly trackRef = viewChild<ElementRef<HTMLDivElement>>('trackEl');
53
+ private moveHandler: ((e: MouseEvent | TouchEvent) => void) | null = null;
54
+ private upHandler: (() => void) | null = null;
55
+
56
+ private _onChange: (value: number) => void = () => {};
57
+ protected onTouched: () => void = () => {};
58
+ private _writing = false;
59
+
60
+ constructor() {
61
+ effect(() => {
62
+ const val = this.value();
63
+ if (this._writing) {
64
+ this._writing = false;
65
+ return;
66
+ }
67
+ this._onChange(val);
68
+ });
69
+ }
70
+
71
+ writeValue(val: number): void {
72
+ this._writing = true;
73
+ this.value.set(val ?? 0);
74
+ }
75
+
76
+ registerOnChange(fn: (value: number) => void): void {
77
+ this._onChange = fn;
78
+ }
79
+
80
+ registerOnTouched(fn: () => void): void {
81
+ this.onTouched = fn;
82
+ }
83
+
84
+ setDisabledState(isDisabled: boolean): void {
85
+ this._disabledByCva.set(isDisabled);
86
+ }
87
+
88
+ protected readonly percentage = computed(() => {
89
+ const range = this.max() - this.min();
90
+ if (range <= 0) return 0;
91
+ return ((this.value() - this.min()) / range) * 100;
92
+ });
93
+
94
+ protected readonly trackClass = computed(() =>
95
+ cn(sliderTrackVariants({ size: this.size() }), this.isDisabled() && 'opacity-50 cursor-not-allowed', this.class())
96
+ );
97
+
98
+ protected readonly thumbClass = computed(() =>
99
+ cn(
100
+ 'absolute top-1/2 -translate-x-1/2 -translate-y-1/2 block rounded-full border-2 border-primary bg-background shadow ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
101
+ sliderThumbSize[this.size()]
102
+ )
103
+ );
104
+
105
+ private updateValueFromPosition(clientX: number): void {
106
+ if (this.isDisabled()) return;
107
+ const track = this.trackRef()?.nativeElement;
108
+ if (!track) return;
109
+ const rect = track.getBoundingClientRect();
110
+ const percent = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
111
+ const raw = this.min() + percent * (this.max() - this.min());
112
+ const stepped = Math.round(raw / this.step()) * this.step();
113
+ this.value.set(Math.max(this.min(), Math.min(this.max(), stepped)));
114
+ }
115
+
116
+ onTrackMousedown(event: MouseEvent): void {
117
+ if (this.isDisabled()) return;
118
+ event.preventDefault();
119
+ this.updateValueFromPosition(event.clientX);
120
+ this.moveHandler = (e: MouseEvent | TouchEvent) => {
121
+ const clientX = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;
122
+ this.updateValueFromPosition(clientX);
123
+ };
124
+ this.upHandler = () => {
125
+ this.onTouched();
126
+ this.removeListeners();
127
+ };
128
+ document.addEventListener('mousemove', this.moveHandler as EventListener);
129
+ document.addEventListener('mouseup', this.upHandler);
130
+ }
131
+
132
+ onTrackTouchstart(event: TouchEvent): void {
133
+ if (this.isDisabled()) return;
134
+ this.updateValueFromPosition(event.touches[0].clientX);
135
+ this.moveHandler = (e: MouseEvent | TouchEvent) => {
136
+ const clientX = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;
137
+ this.updateValueFromPosition(clientX);
138
+ };
139
+ this.upHandler = () => {
140
+ this.onTouched();
141
+ this.removeListeners();
142
+ };
143
+ document.addEventListener('touchmove', this.moveHandler as EventListener, { passive: true });
144
+ document.addEventListener('touchend', this.upHandler);
145
+ }
146
+
147
+ onKeydown(event: KeyboardEvent): void {
148
+ if (this.isDisabled()) return;
149
+ const step = this.step();
150
+ switch (event.key) {
151
+ case 'ArrowRight':
152
+ case 'ArrowUp':
153
+ event.preventDefault();
154
+ this.value.set(Math.min(this.max(), this.value() + step));
155
+ break;
156
+ case 'ArrowLeft':
157
+ case 'ArrowDown':
158
+ event.preventDefault();
159
+ this.value.set(Math.max(this.min(), this.value() - step));
160
+ break;
161
+ case 'Home':
162
+ event.preventDefault();
163
+ this.value.set(this.min());
164
+ break;
165
+ case 'End':
166
+ event.preventDefault();
167
+ this.value.set(this.max());
168
+ break;
169
+ }
170
+ }
171
+
172
+ private removeListeners(): void {
173
+ if (this.moveHandler) {
174
+ document.removeEventListener('mousemove', this.moveHandler as EventListener);
175
+ document.removeEventListener('touchmove', this.moveHandler as EventListener);
176
+ this.moveHandler = null;
177
+ }
178
+ if (this.upHandler) {
179
+ document.removeEventListener('mouseup', this.upHandler);
180
+ document.removeEventListener('touchend', this.upHandler);
181
+ this.upHandler = null;
182
+ }
183
+ }
184
+
185
+ ngOnDestroy(): void {
186
+ this.removeListeners();
187
+ }
188
+ }
@@ -0,0 +1,25 @@
1
+ import { cva } from 'class-variance-authority';
2
+
3
+ export const sliderTrackVariants = cva(
4
+ 'relative w-full rounded-full bg-secondary cursor-pointer',
5
+ {
6
+ variants: {
7
+ size: {
8
+ sm: 'h-1.5',
9
+ md: 'h-2',
10
+ lg: 'h-3',
11
+ },
12
+ },
13
+ defaultVariants: {
14
+ size: 'md',
15
+ },
16
+ }
17
+ );
18
+
19
+ export const sliderThumbSize: Record<string, string> = {
20
+ sm: 'h-4 w-4',
21
+ md: 'h-5 w-5',
22
+ lg: 'h-6 w-6',
23
+ };
24
+
25
+ export type SliderSize = 'sm' | 'md' | 'lg';
@@ -0,0 +1,8 @@
1
+ export {
2
+ SnyStatDirective,
3
+ SnyStatTitleDirective,
4
+ SnyStatValueDirective,
5
+ SnyStatDescriptionDirective,
6
+ SnyStatFigureDirective,
7
+ type StatDescriptionVariant,
8
+ } from './stat.directives';