@recursica/mantine-adapter 0.22.0 → 0.23.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.
@@ -1,12 +1,60 @@
1
1
  import type { Meta, StoryObj } from "@storybook/react";
2
2
  import { Slider } from "./Slider";
3
- import { ComingSoon } from "@recursica/storybook-template";
3
+ import { formControlArgTypes } from "../../../.storybook/commonArgTypes";
4
4
 
5
5
  const meta: Meta<typeof Slider> = {
6
- title: "UI-Kit/🚧 Slider",
6
+ title: "UI-Kit/Slider",
7
7
  component: Slider,
8
8
  tags: ["autodocs"],
9
- argTypes: {},
9
+ parameters: {
10
+ docs: {
11
+ description: {
12
+ component: `
13
+ The \`Slider\` component wraps Mantine's Slider to support a premium design system, a bidirectional input state,
14
+ custom min/max labels, step indicators, and strict states like error, disabled, and read-only.
15
+
16
+ ### Architecture
17
+ The component uses Recursica's unified \`FormControlWrapper\` and \`<WithReadOnlyWrapper>\` to render consistent form layouts (stacked, side-by-side) and read-only representations.
18
+ All visual properties map perfectly to token values inside \`Slider.module.css\`.
19
+ `,
20
+ },
21
+ },
22
+ },
23
+ argTypes: {
24
+ ...formControlArgTypes,
25
+ disabled: {
26
+ control: "boolean",
27
+ description: "Disables both the slider track and the text input field.",
28
+ },
29
+ error: {
30
+ control: "text",
31
+ description:
32
+ "Places the slider in an error state with custom border/icon highlights.",
33
+ },
34
+ required: {
35
+ control: "boolean",
36
+ },
37
+ label: {
38
+ control: "text",
39
+ description: "Outer form control label.",
40
+ },
41
+ assistiveText: {
42
+ control: "text",
43
+ description: "Assistive text rendered below or beside the component.",
44
+ },
45
+ readOnly: {
46
+ control: "boolean",
47
+ description: "Puts the component in static read-only mode.",
48
+ },
49
+ showInput: {
50
+ control: "boolean",
51
+ description: "Controls whether the numeric input box is visible.",
52
+ },
53
+ showMinMaxLabels: {
54
+ control: "boolean",
55
+ description: "Toggles rendering of min/max labels below the track.",
56
+ },
57
+ },
10
58
  };
11
59
 
12
60
  export default meta;
@@ -14,5 +62,112 @@ export default meta;
14
62
  type Story = StoryObj<typeof Slider>;
15
63
 
16
64
  export const Default: Story = {
17
- render: () => <ComingSoon componentName="Slider" />,
65
+ args: {
66
+ label: "Auditory Threshold",
67
+ assistiveText: "Specify the maximum decibel frequency boundary.",
68
+ defaultValue: 60,
69
+ min: 10,
70
+ max: 100,
71
+ step: 1,
72
+ showMinMaxLabels: true,
73
+ },
74
+ };
75
+
76
+ export const WithInputField: Story = {
77
+ args: {
78
+ ...Default.args,
79
+ showInput: true,
80
+ },
81
+ };
82
+
83
+ export const SideBySideLayout: Story = {
84
+ args: {
85
+ ...Default.args,
86
+ formLayout: "side-by-side",
87
+ },
88
+ };
89
+
90
+ export const Disabled: Story = {
91
+ args: {
92
+ label: "Decommissioned Server Node",
93
+ assistiveText: "Modifications to this environment are frozen.",
94
+ defaultValue: 35,
95
+ disabled: false,
96
+ },
97
+ };
98
+
99
+ export const ErrorState: Story = {
100
+ args: {
101
+ label: "Core Temperature Alert",
102
+ assistiveText:
103
+ "Severe core degradation across the hypervisor socket cluster.",
104
+ defaultValue: 85,
105
+ error: "Thermal overload threshold exceeded.",
106
+ required: true,
107
+ },
108
+ };
109
+
110
+ export const StaticReadOnly: Story = {
111
+ args: {
112
+ label: "System Calibration Metrics",
113
+ assistiveText:
114
+ "Frozen baseline calibrations derived during initial staging.",
115
+ value: 65,
116
+ readOnly: true,
117
+ },
118
+ };
119
+
120
+ export const EditableReadOnly: Story = {
121
+ args: {
122
+ label: "Adaptive Node Output",
123
+ assistiveText:
124
+ "Click edit to unlock bidirectional input parameter boundaries.",
125
+ defaultValue: 15,
126
+ readOnly: true,
127
+ labelWithEditIcon: true,
128
+ },
129
+ };
130
+
131
+ export const WithMarks: Story = {
132
+ args: {
133
+ label: "Interactive Marks Map",
134
+ defaultValue: 50,
135
+ min: 0,
136
+ max: 100,
137
+ step: 10,
138
+ marks: [
139
+ { value: 0, label: "0%" },
140
+ { value: 25, label: "25%" },
141
+ { value: 50, label: "50%" },
142
+ { value: 75, label: "75%" },
143
+ { value: 100, label: "100%" },
144
+ ],
145
+ showMinMaxLabels: false,
146
+ },
147
+ };
148
+
149
+ export const FormLayouts: Story = {
150
+ render: () => (
151
+ <div
152
+ style={{
153
+ display: "flex",
154
+ flexDirection: "column",
155
+ gap: "2.5rem",
156
+ maxWidth: 600,
157
+ }}
158
+ >
159
+ <Slider
160
+ label="Stacked Layout"
161
+ assistiveText="This is the standard top-to-bottom stacked form layout."
162
+ defaultValue={40}
163
+ formLayout="stacked"
164
+ />
165
+ <Slider
166
+ label="Side-by-Side Layout"
167
+ assistiveText="This is the side-by-side layout aligning label beside control."
168
+ defaultValue={60}
169
+ formLayout="side-by-side"
170
+ />
171
+ </div>
172
+ ),
18
173
  };
@@ -1,7 +1,286 @@
1
- import React from "react";
1
+ import React, { forwardRef, useState, useEffect } from "react";
2
+ import {
3
+ Slider as MantineSlider,
4
+ type SliderProps as MantineSliderProps,
5
+ type InputWrapperProps,
6
+ } from "@mantine/core";
7
+ import { type ReadOnlyControlProps } from "@recursica/adapter-common";
8
+ import {
9
+ filterStylingProps,
10
+ type RecursicaOverStyled,
11
+ } from "../../utils/filterStylingProps";
12
+ import { type RecursicaFormControlWrapperProps } from "../FormControlWrapper/FormControlWrapper";
13
+ import { WithReadOnlyWrapper } from "../ReadOnlyField/WithReadOnlyWrapper";
2
14
 
3
- export type SliderProps = React.HTMLAttributes<HTMLDivElement>;
15
+ import styles from "./Slider.module.css";
4
16
 
5
- export const Slider: React.FC<SliderProps> = (props) => {
6
- return <div {...props}>Slider</div>;
17
+ export interface RecursicaSliderProps
18
+ extends Omit<
19
+ MantineSliderProps,
20
+ | "size"
21
+ | "variant"
22
+ | "radius"
23
+ | "wrapperProps"
24
+ | "classNames"
25
+ | "styles"
26
+ | "label"
27
+ >,
28
+ Omit<
29
+ RecursicaFormControlWrapperProps,
30
+ "controlMaxWidth" | "controlMinWidth"
31
+ >,
32
+ Pick<InputWrapperProps, "error" | "required" | "withAsterisk">,
33
+ ReadOnlyControlProps {
34
+ /** The form control label displayed above or beside the slider track. */
35
+ label?: React.ReactNode;
36
+ /** Floating tooltip shown above the thumb when dragging. Replaces Mantine's `label`. */
37
+ tooltipLabel?: MantineSliderProps["label"];
38
+ /** An optional icon rendered to the left of the slider track. */
39
+ icon?: React.ReactNode;
40
+ /** Whether to render a numeric input field side-by-side or stacked with the track. Defaults to false. */
41
+ showInput?: boolean;
42
+ /** Whether to render min and max labels below the track. Defaults to true. */
43
+ showMinMaxLabels?: boolean;
44
+ }
45
+
46
+ export type SliderProps = RecursicaOverStyled<RecursicaSliderProps>;
47
+
48
+ /**
49
+ * Custom Read-Only visual representation of the Slider value.
50
+ * Utilizes component-specific read-only typography variables.
51
+ */
52
+ const SliderReadOnlyValue: React.FC<{ value: number }> = ({ value }) => {
53
+ return <div className={styles.readOnlyValue}>{value}</div>;
7
54
  };
55
+
56
+ /**
57
+ * Recursica Slider component wrapping Mantine's Slider.
58
+ *
59
+ * Implements a bidirectional text input field next to the slider track, responsive layouts,
60
+ * custom typography-bound min/max labels, an optional leading icon, and an explicit read-only layout.
61
+ */
62
+ export const Slider = forwardRef<HTMLDivElement, SliderProps>(
63
+ function Slider(props, ref) {
64
+ const {
65
+ overStyled = false,
66
+ formLayout = "stacked",
67
+
68
+ // Label & Wrapper Maps
69
+ labelSize,
70
+ labelAlignment,
71
+ labelOptionalText,
72
+ labelWithEditIcon,
73
+ labelActionArea,
74
+ onLabelEditClick,
75
+
76
+ label,
77
+ tooltipLabel,
78
+ assistiveText,
79
+ assistiveWithIcon,
80
+ error,
81
+ required,
82
+ withAsterisk,
83
+ id,
84
+ className,
85
+ style,
86
+ disabled,
87
+ readOnly,
88
+ readOnlyComponent,
89
+ emptyValueComponent,
90
+ value,
91
+ defaultValue,
92
+ icon,
93
+ showInput = false,
94
+ showMinMaxLabels = true,
95
+ min = 0,
96
+ max = 100,
97
+ step = 1,
98
+ onChange,
99
+ onChangeEnd,
100
+ ...rest
101
+ } = props;
102
+
103
+ // Bidirectional state linking the slider track value to the input field string representation
104
+ const [internalValue, setInternalValue] = useState<number>(() => {
105
+ if (value !== undefined) return value;
106
+ if (defaultValue !== undefined) return defaultValue;
107
+ return min;
108
+ });
109
+
110
+ const resolvedValue = value !== undefined ? value : internalValue;
111
+ const [inputValue, setInputValue] = useState<string>(
112
+ resolvedValue.toString(),
113
+ );
114
+
115
+ // Synchronize text input whenever the slider value changes
116
+ useEffect(() => {
117
+ setInputValue(resolvedValue.toString());
118
+ }, [resolvedValue]);
119
+
120
+ const handleValueChange = (val: number) => {
121
+ if (value === undefined) {
122
+ setInternalValue(val);
123
+ }
124
+ onChange?.(val);
125
+ };
126
+
127
+ const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
128
+ const valStr = e.target.value;
129
+ setInputValue(valStr);
130
+
131
+ const parsed = parseFloat(valStr);
132
+ if (!isNaN(parsed)) {
133
+ // Clamp input value to bounds
134
+ const clamped = Math.max(min, Math.min(max, parsed));
135
+ handleValueChange(clamped);
136
+ }
137
+ };
138
+
139
+ const handleInputBlur = () => {
140
+ // Clean up text field on blur to reflect the final clamped resolved value
141
+ setInputValue(resolvedValue.toString());
142
+ };
143
+
144
+ const sanitizedProps = filterStylingProps(rest, overStyled);
145
+ const restRecord = sanitizedProps as Record<string, unknown>;
146
+
147
+ // Delete prohibited sizing hooks from bypassing variables natively
148
+ delete restRecord["size"];
149
+ delete restRecord["variant"];
150
+ delete restRecord["radius"];
151
+ delete restRecord["wrapperProps"];
152
+
153
+ // Securely map core native blocks down ensuring nested CSS modules map precisely
154
+ const mergedClassNames: Partial<Record<string, string>> = {
155
+ root: styles.sliderRoot,
156
+ track: styles.sliderTrack,
157
+ bar: styles.sliderBar,
158
+ thumb: styles.sliderThumb,
159
+ mark: styles.sliderMark,
160
+ markLabel: styles.sliderMarkLabel,
161
+ };
162
+
163
+ const classNamesProp = restRecord.classNames;
164
+ if (
165
+ classNamesProp &&
166
+ typeof classNamesProp === "object" &&
167
+ !Array.isArray(classNamesProp)
168
+ ) {
169
+ const o = classNamesProp as Partial<Record<string, string>>;
170
+ mergedClassNames.root = o.root
171
+ ? `${styles.sliderRoot} ${o.root}`
172
+ : styles.sliderRoot;
173
+ mergedClassNames.track = o.track
174
+ ? `${styles.sliderTrack} ${o.track}`
175
+ : styles.sliderTrack;
176
+ mergedClassNames.bar = o.bar
177
+ ? `${styles.sliderBar} ${o.bar}`
178
+ : styles.sliderBar;
179
+ mergedClassNames.thumb = o.thumb
180
+ ? `${styles.sliderThumb} ${o.thumb}`
181
+ : styles.sliderThumb;
182
+ mergedClassNames.mark = o.mark
183
+ ? `${styles.sliderMark} ${o.mark}`
184
+ : styles.sliderMark;
185
+ mergedClassNames.markLabel = o.markLabel
186
+ ? `${styles.sliderMarkLabel} ${o.markLabel}`
187
+ : styles.sliderMarkLabel;
188
+ }
189
+
190
+ const wrapperClass = className
191
+ ? `${styles.layoutOverride} ${className}`
192
+ : styles.layoutOverride;
193
+
194
+ const leadingIcon = icon ? (
195
+ <span className={styles.iconWrapper} aria-hidden>
196
+ {icon}
197
+ </span>
198
+ ) : null;
199
+
200
+ return (
201
+ <WithReadOnlyWrapper
202
+ ref={ref}
203
+ className={wrapperClass}
204
+ style={style as React.CSSProperties}
205
+ controlMaxWidth={undefined}
206
+ controlMinWidth={undefined}
207
+ overStyled={overStyled as true}
208
+ formLayout={formLayout}
209
+ labelSize={labelSize}
210
+ labelAlignment={labelAlignment}
211
+ labelOptionalText={labelOptionalText}
212
+ labelWithEditIcon={labelWithEditIcon}
213
+ labelActionArea={labelActionArea}
214
+ onLabelEditClick={onLabelEditClick}
215
+ label={label}
216
+ assistiveText={assistiveText}
217
+ assistiveWithIcon={assistiveWithIcon}
218
+ error={error}
219
+ required={required}
220
+ withAsterisk={withAsterisk}
221
+ id={id}
222
+ readOnly={readOnly}
223
+ readOnlyComponent={readOnlyComponent || SliderReadOnlyValue}
224
+ emptyValueComponent={emptyValueComponent}
225
+ readOnlyType="text"
226
+ readOnlyValue={resolvedValue}
227
+ readOnlyNativeProps={{ value: resolvedValue }}
228
+ activeComponent={
229
+ <div
230
+ className={styles.sliderContainer}
231
+ data-form-layout={formLayout}
232
+ data-disabled={disabled ? "true" : undefined}
233
+ data-error={error ? "true" : undefined}
234
+ >
235
+ {leadingIcon}
236
+
237
+ {showMinMaxLabels && (
238
+ <span className={styles.minMaxGuide}>{min}</span>
239
+ )}
240
+
241
+ <div className={styles.sliderTrackWrapper}>
242
+ <MantineSlider
243
+ classNames={mergedClassNames}
244
+ disabled={disabled}
245
+ value={resolvedValue}
246
+ onChange={handleValueChange}
247
+ onChangeEnd={onChangeEnd}
248
+ min={min}
249
+ max={max}
250
+ step={step}
251
+ label={tooltipLabel}
252
+ {...(sanitizedProps as unknown as MantineSliderProps)}
253
+ />
254
+ </div>
255
+
256
+ <div className={styles.rightGuideContainer}>
257
+ {!showInput && (
258
+ <span className={styles.currentValue}>{resolvedValue}</span>
259
+ )}
260
+ {showMinMaxLabels && (
261
+ <span className={styles.minMaxGuide}>{max}</span>
262
+ )}
263
+ </div>
264
+
265
+ {showInput && (
266
+ <input
267
+ type="number"
268
+ className={styles.inputField}
269
+ value={inputValue}
270
+ onChange={handleInputChange}
271
+ onBlur={handleInputBlur}
272
+ min={min}
273
+ max={max}
274
+ step={step}
275
+ disabled={disabled}
276
+ data-error={error ? "true" : undefined}
277
+ />
278
+ )}
279
+ </div>
280
+ }
281
+ />
282
+ );
283
+ },
284
+ );
285
+
286
+ Slider.displayName = "Slider";