@studiocms/ui 0.1.0 → 0.3.0

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.
@@ -6,6 +6,11 @@ class ModalHelper {
6
6
  private isForm = false;
7
7
  private modalForm: HTMLFormElement;
8
8
 
9
+ /**
10
+ * A helper to manage modals.
11
+ * @param id The ID of the modal.
12
+ * @param triggerID The ID of the element that should trigger the modal.
13
+ */
9
14
  constructor(id: string, triggerID?: string) {
10
15
  const element = document.getElementById(id) as HTMLDialogElement;
11
16
 
@@ -32,6 +37,11 @@ class ModalHelper {
32
37
  }
33
38
  }
34
39
 
40
+ /**
41
+ * A helper function which adds event listeners to the modal buttons to close the modal when clicked.
42
+ * @param id The ID of the modal.
43
+ * @param dismissable Whether the modal is dismissable.
44
+ */
35
45
  private addButtonListeners = (id: string, dismissable: boolean) => {
36
46
  if (
37
47
  dismissable ||
@@ -54,6 +64,9 @@ class ModalHelper {
54
64
  }
55
65
  };
56
66
 
67
+ /**
68
+ * A helper function to close the modal when the user clicks outside of it.
69
+ */
57
70
  private addDismissiveClickListener = () => {
58
71
  this.element.addEventListener('click', (e: MouseEvent) => {
59
72
  if (!e.target) return;
@@ -69,14 +82,25 @@ class ModalHelper {
69
82
  });
70
83
  };
71
84
 
85
+ /**
86
+ * A function to show the modal.
87
+ */
72
88
  public show = () => {
73
89
  this.element.showModal();
90
+ this.element.focus();
74
91
  };
75
92
 
93
+ /**
94
+ * A function to hide the modal.
95
+ */
76
96
  public hide = () => {
77
97
  this.element.close();
78
98
  };
79
99
 
100
+ /**
101
+ * A function to add another trigger to show the modal with.
102
+ * @param elementID The ID of the element that should trigger the modal when clicked.
103
+ */
80
104
  public bindTrigger = (elementID: string) => {
81
105
  const element = document.getElementById(elementID);
82
106
 
@@ -87,6 +111,10 @@ class ModalHelper {
87
111
  element.addEventListener('click', this.show);
88
112
  };
89
113
 
114
+ /**
115
+ * Registers a callback for the cancel button.
116
+ * @param func The callback function.
117
+ */
90
118
  public registerCancelCallback = (func: () => void) => {
91
119
  if (!this.cancelButton) {
92
120
  throw new Error('Unable to register cancel callback without a cancel button.');
@@ -100,6 +128,11 @@ class ModalHelper {
100
128
  });
101
129
  };
102
130
 
131
+ /**
132
+ * Registers a callback for the confirm button.
133
+ * @param func The callback function. If the modal is a form, the function will be called with
134
+ * the form data as the first argument.
135
+ */
103
136
  public registerConfirmCallback = (func: (data?: FormData | undefined) => void) => {
104
137
  if (!this.confirmButton) {
105
138
  throw new Error('Unable to register cancel callback without a confirmation button.');
@@ -2,33 +2,75 @@
2
2
  import type { StudioCMSColorway } from '../utils/colors';
3
3
  import { generateID } from '../utils/generateID';
4
4
 
5
+ /**
6
+ * The props for the RadioGroup component.
7
+ */
5
8
  interface Option {
9
+ /**
10
+ * The label of the option.
11
+ */
6
12
  label: string;
13
+ /**
14
+ * The value of the option.
15
+ */
7
16
  value: string;
17
+ /**
18
+ * Whether the option is disabled.
19
+ */
8
20
  disabled?: boolean;
9
21
  }
10
22
 
23
+ /**
24
+ * The props for the RadioGroup component.
25
+ */
11
26
  interface Props {
27
+ /**
28
+ * The label of the radio group.
29
+ */
12
30
  label: string;
31
+ /**
32
+ * The color of the radio group. Defaults to `default`.
33
+ */
13
34
  color?: StudioCMSColorway;
35
+ /**
36
+ * The default value of the radio group. Needs to be one of the values in the options.
37
+ */
14
38
  defaultValue?: string;
39
+ /**
40
+ * The options to display in the radio group.
41
+ */
15
42
  options: Option[];
43
+ /**
44
+ * Whether the radio group is disabled. Defaults to `false`.
45
+ */
16
46
  disabled?: boolean;
47
+ /**
48
+ * The name of the radio group.
49
+ */
17
50
  name?: string;
51
+ /**
52
+ * Whether the radio group is required. Defaults to `false`.
53
+ */
18
54
  isRequired?: boolean;
55
+ /**
56
+ * Whether the radio group is horizontal. Defaults to `false`.
57
+ */
19
58
  horizontal?: boolean;
59
+ /**
60
+ * Additional classes to apply to the radio group.
61
+ */
20
62
  class?: string;
21
63
  }
22
64
 
23
65
  const {
24
66
  label,
25
- color,
67
+ color = 'default',
26
68
  defaultValue,
27
69
  options,
28
- disabled,
70
+ disabled = false,
71
+ isRequired = false,
72
+ horizontal = false,
29
73
  name = generateID('radio'),
30
- isRequired,
31
- horizontal,
32
74
  class: className,
33
75
  } = Astro.props;
34
76
  ---
@@ -45,18 +87,24 @@ const {
45
87
  <span>
46
88
  {label} <span class="req-star">{isRequired && "*"}</span>
47
89
  </span>
48
- <div class="sui-radio-inputs">
49
- {options.map(({ label, value, disabled: individuallyDisabled }) => (
90
+ <div class="sui-radio-inputs" role="radiogroup">
91
+ {options.map(({ label, value, disabled: individuallyDisabled }, i) => (
50
92
  <label
51
93
  for={value}
52
94
  class="sui-radio-label"
53
95
  class:list={[ individuallyDisabled && "disabled" ]}
54
96
  >
55
97
  <div class="sui-radio-box-container">
56
- <div class="sui-radio-box" />
98
+ <div
99
+ class="sui-radio-box"
100
+ role="radio"
101
+ tabindex={i === 0 ? 0 : -1}
102
+ aria-checked={value === defaultValue}
103
+ aria-label={label}
104
+ />
57
105
  </div>
58
106
  <input
59
- class="sui-radio-toggle"
107
+ class="sui-radio-toggle"
60
108
  type="radio"
61
109
  value={value}
62
110
  id={value}
@@ -70,6 +118,78 @@ const {
70
118
  ))}
71
119
  </div>
72
120
  </div>
121
+ <script>
122
+ const elements = document.querySelectorAll<HTMLDivElement>('.sui-radio-container');
123
+
124
+ for (const element of elements) {
125
+ if (element.dataset.initialized) continue;
126
+
127
+ element.dataset.initialized = 'true';
128
+
129
+ const radioBoxes = element.querySelectorAll<HTMLDivElement>('.sui-radio-box');
130
+
131
+ let i = 0;
132
+
133
+ for (const radioBox of radioBoxes) {
134
+ radioBox.addEventListener('keydown', (e) => {
135
+ if (e.key === 'Enter' || e.key === " ") {
136
+ e.preventDefault();
137
+
138
+ const input = (e.target as HTMLDivElement).parentElement!.parentElement!.querySelector<HTMLInputElement>('.sui-radio-toggle')!;
139
+
140
+ if (input.disabled) return;
141
+
142
+ input.checked = true;
143
+ }
144
+
145
+ if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
146
+ e.preventDefault();
147
+
148
+ let nextRadioBox: HTMLDivElement | undefined;
149
+
150
+ radioBoxes.forEach((box, index) => {
151
+ if (box === radioBox) nextRadioBox = radioBoxes[index + 1];
152
+ });
153
+
154
+ if (!nextRadioBox) return;
155
+
156
+ radioBox.tabIndex = -1;
157
+ nextRadioBox.tabIndex = 0;
158
+ nextRadioBox.focus();
159
+ nextRadioBox.click();
160
+ }
161
+
162
+ if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
163
+ e.preventDefault();
164
+
165
+ let previousRadioBox: HTMLDivElement | undefined;
166
+
167
+ radioBoxes.forEach((box, index) => {
168
+ if (box === radioBox) previousRadioBox = radioBoxes[index - 1];
169
+ });
170
+
171
+ if (!previousRadioBox) return;
172
+
173
+ radioBox.tabIndex = -1;
174
+ previousRadioBox.tabIndex = 0;
175
+ previousRadioBox.focus();
176
+ previousRadioBox.click();
177
+ }
178
+ });
179
+
180
+ i++;
181
+ }
182
+ element.addEventListener('keydown', (e) => {
183
+ if (e.key !== 'Enter') return;
184
+
185
+ const checkbox = element.querySelector<HTMLInputElement>('.sui-checkbox');
186
+
187
+ if (!checkbox) return;
188
+
189
+ checkbox.click();
190
+ });
191
+ }
192
+ </script>
73
193
  <style>
74
194
  .sui-radio-container {
75
195
  display: flex;
@@ -164,6 +284,10 @@ const {
164
284
  transition: all .15s ease;
165
285
  }
166
286
 
287
+ .sui-radio-box:focus-visible {
288
+ outline-color: hsl(var(--text-normal)) !important;
289
+ }
290
+
167
291
  .sui-radio-toggle {
168
292
  width: 0;
169
293
  height: 0;
@@ -1,8 +1,17 @@
1
1
  ---
2
2
  import type { HTMLAttributes } from 'astro/types';
3
3
 
4
+ /**
5
+ * The props for the row component.
6
+ */
4
7
  interface Props extends HTMLAttributes<'div'> {
8
+ /**
9
+ * Whether the row should be aligned to the center. Defaults to `false`.
10
+ */
5
11
  alignCenter?: boolean;
12
+ /**
13
+ * The size of the gap between the children. Defaults to `md`.
14
+ */
6
15
  gapSize?: 'sm' | 'md' | 'lg';
7
16
  }
8
17