etudes 0.29.0 → 0.34.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.
package/lib/Slider.d.ts CHANGED
@@ -1,36 +1,24 @@
1
1
  import { CSSProperties } from 'react';
2
2
  import { CSSProp } from 'styled-components';
3
3
  import { Orientation } from './types';
4
- export declare type GutterCSSProps = Readonly<{
5
- orientation: Orientation;
6
- }>;
7
- export declare type LabelCSSProps = Readonly<{
8
- knobHeight: number;
9
- }>;
10
4
  export declare type Props = {
11
5
  id?: string;
12
6
  className?: string;
13
7
  style?: CSSProperties;
14
8
  isInverted?: boolean;
15
9
  onlyDispatchesOnDragEnd?: boolean;
16
- isLabelVisible?: boolean;
17
- labelProvider?: (position: number, index: number) => string;
10
+ labelProvider?: (position: number) => string;
18
11
  gutterPadding?: number;
19
12
  knobHeight?: number;
20
13
  knobWidth?: number;
21
14
  orientation?: Orientation;
22
- defaultPosition?: number;
15
+ position?: number;
16
+ onPositionChange?: (position: number, isDragging: boolean) => void;
23
17
  onDragEnd?: () => void;
24
18
  onDragStart?: () => void;
25
- onPositionChange?: (position: number) => void;
26
19
  startingGutterCSS?: CSSProp<any>;
27
20
  endingGutterCSS?: CSSProp<any>;
28
21
  knobCSS?: CSSProp<any>;
29
22
  labelCSS?: CSSProp<any>;
30
- breakpoints?: readonly number[];
31
- autoSnap?: boolean;
32
- defaultIndex?: number;
33
- onIndexChange?: (index: number) => void;
34
23
  };
35
- export declare function generateBreakpoints(length: number): readonly number[];
36
- export default function Slider({ id, className, style, isInverted, onlyDispatchesOnDragEnd, gutterPadding, knobHeight, knobWidth, orientation, defaultPosition, isLabelVisible, labelProvider, onDragEnd, onDragStart, onPositionChange, startingGutterCSS, endingGutterCSS, knobCSS, labelCSS, breakpoints, autoSnap, defaultIndex, onIndexChange, }: Props): JSX.Element;
24
+ export default function Slider({ id, className, style, isInverted, onlyDispatchesOnDragEnd, gutterPadding, knobHeight, knobWidth, orientation, position, labelProvider, onDragEnd, onDragStart, onPositionChange, startingGutterCSS, endingGutterCSS, knobCSS, labelCSS, }: Props): JSX.Element;
package/lib/Slider.js CHANGED
@@ -53,54 +53,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
53
53
  return (mod && mod.__esModule) ? mod : { "default": mod };
54
54
  };
55
55
  Object.defineProperty(exports, "__esModule", { value: true });
56
- exports.generateBreakpoints = void 0;
57
56
  var classnames_1 = __importDefault(require("classnames"));
58
57
  var interactjs_1 = __importDefault(require("interactjs"));
59
58
  var react_1 = __importStar(require("react"));
60
59
  var spase_1 = require("spase");
61
60
  var styled_components_1 = __importStar(require("styled-components"));
62
- function generateBreakpoints(length) {
63
- if (length <= 1)
64
- throw new Error('`length` value must be greater than or equal to 2');
65
- if (Math.round(length) !== length)
66
- throw new Error('`length` value must be an integer');
67
- var interval = 1 / (length - 1);
68
- return Array(length).fill(null).map(function (v, i) {
69
- var pos = interval * i;
70
- return pos;
71
- });
72
- }
73
- exports.generateBreakpoints = generateBreakpoints;
61
+ var debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:slider') : function () { };
74
62
  function Slider(_a) {
75
- var id = _a.id, className = _a.className, style = _a.style, _b = _a.isInverted, isInverted = _b === void 0 ? false : _b, _c = _a.onlyDispatchesOnDragEnd, onlyDispatchesOnDragEnd = _c === void 0 ? false : _c, _d = _a.gutterPadding, gutterPadding = _d === void 0 ? 0 : _d, _e = _a.knobHeight, knobHeight = _e === void 0 ? 30 : _e, _f = _a.knobWidth, knobWidth = _f === void 0 ? 30 : _f, _g = _a.orientation, orientation = _g === void 0 ? 'vertical' : _g, _h = _a.defaultPosition, defaultPosition = _h === void 0 ? 0 : _h, _j = _a.isLabelVisible, isLabelVisible = _j === void 0 ? true : _j, labelProvider = _a.labelProvider, onDragEnd = _a.onDragEnd, onDragStart = _a.onDragStart, onPositionChange = _a.onPositionChange, startingGutterCSS = _a.startingGutterCSS, endingGutterCSS = _a.endingGutterCSS, knobCSS = _a.knobCSS, labelCSS = _a.labelCSS, breakpoints = _a.breakpoints, _k = _a.autoSnap, autoSnap = _k === void 0 ? false : _k, defaultIndex = _a.defaultIndex, onIndexChange = _a.onIndexChange;
76
- function setLivePosition(value) {
77
- livePosition.current = value;
78
- setPosition(value);
79
- }
80
- function getClosestIndex() {
81
- if (!breakpoints)
82
- return -1;
83
- var index = 0;
84
- var minDelta = NaN;
85
- for (var i = 0, n = breakpoints.length; i < n; i++) {
86
- var breakpoint = getPositionByIndex(i);
87
- var delta = Math.abs(position - breakpoint);
88
- if (isNaN(minDelta) || (delta < minDelta)) {
89
- minDelta = delta;
90
- index = i;
91
- }
92
- }
93
- return index;
94
- }
95
- function getPositionByIndex(index) {
96
- if (!breakpoints)
97
- return NaN;
98
- return breakpoints[index];
99
- }
63
+ var id = _a.id, className = _a.className, style = _a.style, _b = _a.isInverted, isInverted = _b === void 0 ? false : _b, _c = _a.onlyDispatchesOnDragEnd, onlyDispatchesOnDragEnd = _c === void 0 ? false : _c, _d = _a.gutterPadding, gutterPadding = _d === void 0 ? 0 : _d, _e = _a.knobHeight, knobHeight = _e === void 0 ? 30 : _e, _f = _a.knobWidth, knobWidth = _f === void 0 ? 30 : _f, _g = _a.orientation, orientation = _g === void 0 ? 'vertical' : _g, _h = _a.position, position = _h === void 0 ? 0 : _h, labelProvider = _a.labelProvider, onDragEnd = _a.onDragEnd, onDragStart = _a.onDragStart, onPositionChange = _a.onPositionChange, startingGutterCSS = _a.startingGutterCSS, endingGutterCSS = _a.endingGutterCSS, knobCSS = _a.knobCSS, labelCSS = _a.labelCSS;
100
64
  function initInteractivity() {
101
65
  var knob = knobRef.current;
102
66
  if (!knob || interactjs_1.default.isSet(knob))
103
67
  return;
68
+ debug('Initializing interactivity...', 'OK');
104
69
  (0, interactjs_1.default)(knob).draggable({
105
70
  inertia: true,
106
71
  onstart: function () { return onKnobDragStart(); },
@@ -113,42 +78,44 @@ function Slider(_a) {
113
78
  }
114
79
  function deinitInteractivity() {
115
80
  var knob = knobRef.current;
116
- if (!knob)
81
+ if (!knob || !interactjs_1.default.isSet(knob))
117
82
  return;
83
+ debug('Deinitializing interactivity...', 'OK');
118
84
  (0, interactjs_1.default)(knob).unset();
119
85
  }
120
86
  function onKnobDragStart() {
87
+ debug('Handling drag start...', 'OK');
121
88
  setIsDragging(true);
122
89
  onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart();
123
90
  }
124
91
  function onKnobDragMove(delta) {
125
92
  var _a;
126
93
  var rect = (_a = spase_1.Rect.from(rootRef.current)) !== null && _a !== void 0 ? _a : new spase_1.Rect();
127
- var naturalPosition = isInverted ? (1 - livePosition.current) : livePosition.current;
94
+ var naturalPosition = isInverted ? 1 - livePosition.current : livePosition.current;
128
95
  var naturalNewPositionX = naturalPosition * rect.width + delta;
129
96
  var naturalNewPositionY = naturalPosition * rect.height + delta;
130
97
  var naturalNewPosition = (orientation === 'vertical') ? Math.max(0, Math.min(1, naturalNewPositionY / rect.height)) : Math.max(0, Math.min(1, naturalNewPositionX / rect.width));
131
- var newPosition = isInverted ? (1 - naturalNewPosition) : naturalNewPosition;
98
+ var newPosition = isInverted ? 1 - naturalNewPosition : naturalNewPosition;
132
99
  setIsDragging(true);
133
100
  setLivePosition(newPosition);
134
101
  }
135
102
  function onKnobDragStop() {
103
+ debug('Handling drag stop...', 'OK');
136
104
  setIsDragging(false);
137
- snapToClosestBreakpointIfNeeded();
138
105
  onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd();
139
106
  }
140
- function snapToClosestBreakpointIfNeeded() {
141
- if (!autoSnap || !breakpoints)
107
+ function setLivePosition(position) {
108
+ if (livePosition.current === position)
142
109
  return;
143
- var position = getPositionByIndex(getClosestIndex());
144
- setLivePosition(position);
110
+ livePosition.current = position;
111
+ _setPosition(position);
145
112
  }
146
113
  var rootRef = (0, react_1.useRef)(null);
147
114
  var knobRef = (0, react_1.useRef)(null);
148
- var livePosition = (0, react_1.useRef)(((breakpoints !== undefined) && (defaultIndex !== undefined)) ? getPositionByIndex(defaultIndex) : defaultPosition);
149
- var _l = __read((0, react_1.useState)(livePosition.current), 2), position = _l[0], setPosition = _l[1];
150
- var _m = __read((0, react_1.useState)(undefined), 2), isDragging = _m[0], setIsDragging = _m[1];
151
- var naturalPosition = isInverted ? 1 - position : position;
115
+ var livePosition = (0, react_1.useRef)(position);
116
+ var _j = __read((0, react_1.useState)(livePosition.current), 2), _position = _j[0], _setPosition = _j[1];
117
+ var _k = __read((0, react_1.useState)(undefined), 2), isDragging = _k[0], setIsDragging = _k[1];
118
+ var naturalPosition = isInverted ? 1 - _position : _position;
152
119
  (0, react_1.useEffect)(function () {
153
120
  initInteractivity();
154
121
  return function () {
@@ -156,15 +123,23 @@ function Slider(_a) {
156
123
  };
157
124
  }, []);
158
125
  (0, react_1.useEffect)(function () {
159
- snapToClosestBreakpointIfNeeded();
160
- }, [autoSnap]);
161
- (0, react_1.useEffect)(function () {
162
- if (onlyDispatchesOnDragEnd && isDragging)
126
+ if (isDragging === true)
163
127
  return;
164
- onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange(position);
165
- if (breakpoints)
166
- onIndexChange === null || onIndexChange === void 0 ? void 0 : onIndexChange(getClosestIndex());
128
+ if (position === _position)
129
+ return;
130
+ setIsDragging(undefined);
131
+ setLivePosition(position);
167
132
  }, [position]);
133
+ (0, react_1.useEffect)(function () {
134
+ if (isDragging === true && onlyDispatchesOnDragEnd)
135
+ return;
136
+ onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange(_position, isDragging === true);
137
+ }, [_position]);
138
+ (0, react_1.useEffect)(function () {
139
+ if (isDragging === false && onlyDispatchesOnDragEnd) {
140
+ onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange(_position, false);
141
+ }
142
+ }, [isDragging]);
168
143
  return (react_1.default.createElement(StyledRoot, { ref: rootRef, id: id, className: className, orientation: orientation, style: style },
169
144
  react_1.default.createElement(StyledGutter, { orientation: orientation, css: startingGutterCSS, style: orientation === 'vertical' ? {
170
145
  top: 0,
@@ -175,22 +150,21 @@ function Slider(_a) {
175
150
  } }),
176
151
  react_1.default.createElement(StyledKnobContainer, { ref: knobRef, style: __assign({ transform: 'translate3d(-50%, -50%, 0)' }, (orientation === 'vertical' ? {
177
152
  left: '50%',
178
- top: "".concat(position * 100, "%"),
153
+ top: "".concat(naturalPosition * 100, "%"),
179
154
  transition: isDragging === false ? 'top 100ms ease-out' : 'none',
180
155
  } : {
181
- left: "".concat(position * 100, "%"),
156
+ left: "".concat(naturalPosition * 100, "%"),
182
157
  top: '50%',
183
158
  transition: isDragging === false ? 'left 100ms ease-out' : 'none',
184
159
  })) },
185
160
  react_1.default.createElement(StyledKnob, { className: (0, classnames_1.default)({
186
- 'at-end': isInverted ? (position === 0) : (position === 1),
187
- 'at-start': isInverted ? (position === 1) : (position === 0),
161
+ 'at-end': isInverted ? (_position === 0) : (_position === 1),
162
+ 'at-start': isInverted ? (_position === 1) : (_position === 0),
188
163
  'dragging': isDragging === true,
189
- 'idle': isDragging === false,
190
164
  }), css: knobCSS, style: {
191
165
  height: "".concat(knobHeight, "px"),
192
166
  width: "".concat(knobWidth, "px"),
193
- } }, breakpoints && isLabelVisible && labelProvider && (react_1.default.createElement(StyledLabel, { knobHeight: knobHeight, css: labelCSS }, labelProvider(position, getClosestIndex()))))),
167
+ } }, labelProvider && (react_1.default.createElement(StyledLabel, { knobHeight: knobHeight, css: labelCSS }, labelProvider(_position))))),
194
168
  react_1.default.createElement(StyledGutter, { orientation: orientation, css: endingGutterCSS, style: orientation === 'vertical' ? {
195
169
  bottom: 0,
196
170
  height: "calc(".concat((1 - naturalPosition) * 100, "% - ").concat(knobHeight * .5, "px - ").concat(gutterPadding, "px)"),
@@ -200,10 +174,10 @@ function Slider(_a) {
200
174
  } })));
201
175
  }
202
176
  exports.default = Slider;
203
- var StyledGutter = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n background: #fff;\n position: absolute;\n\n ", "\n"], ["\n background: #fff;\n position: absolute;\n\n ", "\n"])), function (props) { return props.orientation === 'vertical' ? (0, styled_components_1.css)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "], ["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "]))) : (0, styled_components_1.css)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "], ["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "]))); });
204
- var StyledLabel = styled_components_1.default.label(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n"], ["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n"])), function (props) { return props.knobHeight * .5; });
205
- var StyledKnobContainer = styled_components_1.default.button(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n position: absolute;\n z-index: 1;\n transition-duration: 100ms;\n transition-property: top;\n transition-timing-function: ease-out;\n"], ["\n position: absolute;\n z-index: 1;\n transition-duration: 100ms;\n transition-property: top;\n transition-timing-function: ease-out;\n"])));
206
- var StyledKnob = styled_components_1.default.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n cursor: pointer;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n &.idle {\n opacity: 1;\n transition-property: background, color, opacity, margin, transform;\n }\n\n &.dragging {\n opacity: .6;\n transition-property: background, color, transform, opacity;\n }\n"], ["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n cursor: pointer;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n &.idle {\n opacity: 1;\n transition-property: background, color, opacity, margin, transform;\n }\n\n &.dragging {\n opacity: .6;\n transition-property: background, color, transform, opacity;\n }\n"])));
177
+ var StyledGutter = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n background: #fff;\n position: absolute;\n\n ", "\n\n ", "\n"], ["\n background: #fff;\n position: absolute;\n\n ", "\n\n ", "\n"])), function (props) { return props.orientation === 'vertical' ? (0, styled_components_1.css)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "], ["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "]))) : (0, styled_components_1.css)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "], ["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "]))); }, function (props) { return props.css; });
178
+ var StyledLabel = styled_components_1.default.label(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n\n ", "\n"], ["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n\n ", "\n"])), function (props) { return props.knobHeight * .5; }, function (props) { return props.css; });
179
+ var StyledKnobContainer = styled_components_1.default.button(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n position: absolute;\n z-index: 1;\n"], ["\n position: absolute;\n z-index: 1;\n"])));
180
+ var StyledKnob = styled_components_1.default.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ", "\n"], ["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ", "\n"])), function (props) { return props.css; });
207
181
  var StyledRoot = styled_components_1.default.div(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"], ["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"])), function (props) { return props.orientation === 'vertical' ? '300px' : '4px'; }, function (props) { return props.orientation === 'vertical' ? '4px' : '300px'; });
208
182
  var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7;
209
183
  //# sourceMappingURL=Slider.js.map
package/lib/Slider.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Slider.js","sourceRoot":"/","sources":["Slider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0DAAmC;AACnC,0DAAiC;AACjC,6CAAyE;AACzE,+BAA4B;AAC5B,qEAAwD;AAgKxD,SAAgB,mBAAmB,CAAC,MAAc;IAChD,IAAI,MAAM,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAA;IACrF,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;IAEvF,IAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAEjC,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAC,CAAC,EAAE,CAAC;QACvC,IAAM,GAAG,GAAG,QAAQ,GAAG,CAAC,CAAA;QACxB,OAAO,GAAG,CAAA;IACZ,CAAC,CAAC,CAAA;AACJ,CAAC;AAVD,kDAUC;AAaD,SAAwB,MAAM,CAAC,EAwBvB;QAvBN,EAAE,QAAA,EACF,SAAS,eAAA,EACT,KAAK,WAAA,EACL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AAED,IAAM,mBAAmB,GAAG,2BAAM,CAAC,MAAM,kNAAA,+IAMxC,IAAA,CAAA;AAED,IAAM,UAAU,GAAG,2BAAM,CAAC,GAAG,gnBAAA,6iBAuB5B,IAAA,CAAA;AAED,IAAM,UAAU,GAAG,2BAAM,CAAC,GAAG,6KAE3B,4DAGU,EAA2D,qCAE5D,EAA2D,KACrE,KAHW,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAlD,CAAkD,EAE5D,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAlD,CAAkD,CACrE,CAAA","sourcesContent":["import classNames from 'classnames'\nimport interact from 'interactjs'\nimport React, { CSSProperties, useEffect, useRef, useState } from 'react'\nimport { Rect } from 'spase'\nimport styled, { css, CSSProp } from 'styled-components'\nimport { Orientation } from './types'\n\nexport type GutterCSSProps = Readonly<{\n orientation: Orientation\n}>\n\nexport type LabelCSSProps = Readonly<{\n knobHeight: number\n}>\n\nexport type Props = {\n /**\n * ID attribute of the root element.\n */\n id?: string\n\n /**\n * Class attribute of the root element.\n */\n className?: string\n\n /**\n * Inline style attribute of the root element.\n */\n style?: CSSProperties\n\n /**\n * By default the position is a value from 0 - 1, 0 being the start of the slider and 1 being the\n * end. Switching on this flag inverts this behavior, where 0 becomes the end of the slider and 1\n * being the start.\n */\n isInverted?: boolean\n\n /**\n * Indicates if position/index change events are dispatched only when dragging ends. When\n * disabled, aforementioned events are fired repeatedly while dragging.\n */\n onlyDispatchesOnDragEnd?: boolean\n\n /**\n * Indicates if the label is visible by the knob. Note that this is only applicable if\n * `labelProvider` is set.\n */\n isLabelVisible?: boolean\n\n /**\n * A function that returns the label to be displayed (if `isLabelVisible` is `true`) at a given\n * slider position and cloest breakpoint index (if breakpoints are provided).\n *\n * @param position - The current slider position.\n * @praam index - The nearest breakpoint index (if breakpoints are provided), or -1 if no\n * breakpoints are provided.\n *\n * @returns The label.\n */\n labelProvider?: (position: number, index: number) => string\n\n /**\n * Padding between the gutter and the knob in pixels.\n */\n gutterPadding?: number\n\n /**\n * Height of the knob in pixels.\n */\n knobHeight?: number\n\n /**\n * Width of the knob in pixels.\n */\n knobWidth?: number\n\n /**\n * Orientation of the slider.\n */\n orientation?: Orientation\n\n /**\n * The default position. This is ignored if `defaultIndex` and breakpoints are provided.\n */\n defaultPosition?: number\n\n /**\n * Handler invoked when dragging ends.\n */\n onDragEnd?: () => void\n\n /**\n * Handler invoked when dragging begins.\n */\n onDragStart?: () => void\n\n /**\n * Handler invoked when position changes.\n *\n * @param position - The current slider position.\n */\n onPositionChange?: (position: number) => void\n\n /**\n * Custom CSS provided to the gutter before the knob.\n */\n startingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the gutter after the knob.\n */\n endingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the knob.\n */\n knobCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the label inside the knob.\n */\n labelCSS?: CSSProp<any>\n\n // The following props are only used if `breakpoints` are provided.\n\n /**\n * An array of breakpoint descriptors. A breakpoint is a position (0 - 1 inclusive) on the gutter\n * where the knob should snap to if dragging stops near it. If breakpoints are to be specified,\n * ensure that there are at least two: one for the start of the gutter and one for the end.\n */\n breakpoints?: readonly number[]\n\n /**\n * Indicates whether the knob automatically snaps to the nearest breakpoint, if breakpoints are\n * provided.\n */\n autoSnap?: boolean\n\n /**\n * The default index. This is only used if breakpoints are provided. On the other hand, if\n * breakpoints are provided, the default position will be calculated based on this value, making\n * `defaultPosition` irrelevant.\n */\n defaultIndex?: number\n\n /**\n * Handler invoked when index changes. This happens simultaneously with `onPositionChange`. Note\n * that this is only invoked if breakpoints are provided, because otherwise there will be no\n * indexes.\n *\n * @param index - The current breakpoint index.\n */\n onIndexChange?: (index: number) => void\n}\n\n/**\n * Generates a set of breakpoints compatible with this component.\n *\n * @param length - The number of breakpoints. This must be at least 2 because you must include the\n * starting and ending points.\n *\n * @returns An array of breakpoints.\n */\nexport function generateBreakpoints(length: number): readonly number[] {\n if (length <= 1) throw new Error('`length` value must be greater than or equal to 2')\n if (Math.round(length) !== length) throw new Error('`length` value must be an integer')\n\n const interval = 1 / (length - 1)\n\n return Array(length).fill(null).map((v, i) => {\n const pos = interval * i\n return pos\n })\n}\n\n/**\n * A slider component that divides the scroll gutter into two different elements—one that is before\n * the knob and one that is after the knob. This allows for individual styling customizations. The\n * width and height of the root element of this component is taken from the aggregated rect of both\n * gutter parts. The dimension of the knob itself does not impact that of the root element. In\n * addition to the tranditional behavior of a scrollbar, this component allows you to provide\n * breakpoints along the gutter so the knob can automatically snap to them (if feature is enabled)\n * when dragging ends near the breakpoint positions. You can also supply a label to each breakpoint\n * and have it display on the knob when the current position is close to the breakpoint. This\n * component supports both horizontal and vertical orientations.\n */\nexport default function Slider({\n id,\n className,\n style,\n isInverted = false,\n onlyDispatchesOnDragEnd = false,\n gutterPadding = 0,\n knobHeight = 30,\n knobWidth = 30,\n orientation = 'vertical',\n defaultPosition = 0,\n isLabelVisible = true,\n labelProvider,\n onDragEnd,\n onDragStart,\n onPositionChange,\n startingGutterCSS,\n endingGutterCSS,\n knobCSS,\n labelCSS,\n breakpoints,\n autoSnap = false,\n defaultIndex,\n onIndexChange,\n}: Props) {\n /**\n * Sets the current position. The position should be normalized. That is, inversion should be\n * taken care of prior to passing the new value to this method if `isInverted` is `true`.\n *\n * @param value - The value to set the position to.\n */\n function setLivePosition(value: number) {\n livePosition.current = value\n setPosition(value)\n }\n\n /**\n * Gets the index of the breakpoint of which the current position is closest to. If for whatever\n * reason the index cannot be computed (i.e. no breakpoints were provided), -1 is returned.\n *\n * @returns The closest index.\n */\n function getClosestIndex(): number {\n if (!breakpoints) return -1\n\n let index = 0\n let minDelta = NaN\n\n for (let i = 0, n = breakpoints.length; i < n; i++) {\n const breakpoint = getPositionByIndex(i)\n const delta = Math.abs(position - breakpoint)\n\n if (isNaN(minDelta) || (delta < minDelta)) {\n minDelta = delta\n index = i\n }\n }\n\n return index\n }\n\n /**\n * Gets the position by breakpoint index. This value ranges between 0 - 1, inclusive.\n *\n * @param index - The breakpoint index.\n *\n * @returns The position. If for whatever reason the position cannot be determined, NaN is\n * returned.\n */\n function getPositionByIndex(index: number): number {\n if (!breakpoints) return NaN\n return breakpoints[index]\n }\n\n /**\n * Initializes input interactivity of the knob.\n */\n function initInteractivity() {\n const knob = knobRef.current\n if (!knob || interact.isSet(knob)) return\n\n interact(knob).draggable({\n inertia: true,\n onstart: () => onKnobDragStart(),\n onmove: ({ dx, dy }) => onKnobDragMove(orientation === 'vertical' ? dy : dx),\n onend: () => onKnobDragStop(),\n })\n }\n\n /**\n * Deinitializes input interactivity of the knob.\n */\n function deinitInteractivity() {\n const knob = knobRef.current\n if (!knob) return\n\n interact(knob).unset()\n }\n\n /**\n * Handler invoked when the knob starts dragging.\n */\n function onKnobDragStart() {\n setIsDragging(true)\n\n onDragStart?.()\n }\n\n /**\n * Handler invoked when the knob moves.\n *\n * @param delta - The distance traveled (in pixels) since the last invocation of this handler.\n */\n function onKnobDragMove(delta: number) {\n const rect = Rect.from(rootRef.current) ?? new Rect()\n const naturalPosition = isInverted ? (1 - livePosition.current) : livePosition.current\n const naturalNewPositionX = naturalPosition * rect.width + delta\n const naturalNewPositionY = naturalPosition * rect.height + delta\n const naturalNewPosition = (orientation === 'vertical') ? Math.max(0, Math.min(1, naturalNewPositionY / rect.height)) : Math.max(0, Math.min(1, naturalNewPositionX / rect.width))\n const newPosition = isInverted ? (1 - naturalNewPosition) : naturalNewPosition\n\n setIsDragging(true)\n setLivePosition(newPosition)\n }\n\n /**\n * Handler invoked when the knob stops dragging.\n */\n function onKnobDragStop() {\n setIsDragging(false)\n snapToClosestBreakpointIfNeeded()\n\n onDragEnd?.()\n }\n\n /**\n * Snaps the knob to the closest breakpoint. Note that if there are no breakpoints or\n * auto-snapping feature is disabled, this method does nothing.\n */\n function snapToClosestBreakpointIfNeeded() {\n if (!autoSnap || !breakpoints) return\n const position = getPositionByIndex(getClosestIndex())\n setLivePosition(position)\n }\n\n const rootRef = useRef<HTMLDivElement>(null)\n const knobRef = useRef<HTMLButtonElement>(null)\n const livePosition = useRef(((breakpoints !== undefined) && (defaultIndex !== undefined)) ? getPositionByIndex(defaultIndex) : defaultPosition)\n\n const [position, setPosition] = useState(livePosition.current)\n const [isDragging, setIsDragging] = useState<boolean | undefined>(undefined)\n\n const naturalPosition = isInverted ? 1 - position : position\n\n useEffect(() => {\n initInteractivity()\n\n return () => {\n deinitInteractivity()\n }\n }, [])\n\n useEffect(() => {\n snapToClosestBreakpointIfNeeded()\n }, [autoSnap])\n\n useEffect(() => {\n if (onlyDispatchesOnDragEnd && isDragging) return\n onPositionChange?.(position)\n if (breakpoints) onIndexChange?.(getClosestIndex())\n }, [position])\n\n return (\n <StyledRoot ref={rootRef} id={id} className={className} orientation={orientation} style={style}>\n <StyledGutter orientation={orientation} css={startingGutterCSS}\n style={orientation === 'vertical' ? {\n top: 0,\n height: `calc(${naturalPosition*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n left: 0,\n width: `calc(${naturalPosition*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n <StyledKnobContainer ref={knobRef} style={{\n transform: 'translate3d(-50%, -50%, 0)',\n ...(orientation === 'vertical' ? {\n left: '50%',\n top: `${position*100}%`,\n transition: isDragging === false ? 'top 100ms ease-out' : 'none',\n } : {\n left: `${position*100}%`,\n top: '50%',\n transition: isDragging === false ? 'left 100ms ease-out' : 'none',\n }),\n }}>\n <StyledKnob\n className={classNames({\n 'at-end': isInverted ? (position === 0) : (position === 1),\n 'at-start': isInverted ? (position === 1) : (position === 0),\n 'dragging': isDragging === true,\n 'idle': isDragging === false,\n })}\n css={knobCSS}\n style={{\n height: `${knobHeight}px`,\n width: `${knobWidth}px`,\n }}\n >\n {breakpoints && isLabelVisible && labelProvider && (\n <StyledLabel knobHeight={knobHeight} css={labelCSS}>{labelProvider(position, getClosestIndex())}</StyledLabel>\n )}\n </StyledKnob>\n </StyledKnobContainer>\n <StyledGutter orientation={orientation} css={endingGutterCSS}\n style={orientation === 'vertical' ? {\n bottom: 0,\n height: `calc(${(1 - naturalPosition)*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n right: 0,\n width: `calc(${(1 - naturalPosition)*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n </StyledRoot>\n )\n}\n\nconst StyledGutter = styled.div<GutterCSSProps>`\n background: #fff;\n position: absolute;\n\n ${props => props.orientation === 'vertical' ? css`\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n ` : css`\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n `}\n`\n\nconst StyledLabel = styled.label<LabelCSSProps>`\n color: #000;\n font-size: ${props => props.knobHeight * .5}px;\n pointer-events: none;\n user-select: none;\n`\n\nconst StyledKnobContainer = styled.button`\n position: absolute;\n z-index: 1;\n transition-duration: 100ms;\n transition-property: top;\n transition-timing-function: ease-out;\n`\n\nconst StyledKnob = styled.div`\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n cursor: pointer;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n &.idle {\n opacity: 1;\n transition-property: background, color, opacity, margin, transform;\n }\n\n &.dragging {\n opacity: .6;\n transition-property: background, color, transform, opacity;\n }\n`\n\nconst StyledRoot = styled.div<{\n orientation: Orientation\n}>`\n box-sizing: border-box;\n display: block;\n height: ${props => props.orientation === 'vertical' ? '300px' : '4px'};\n position: relative;\n width: ${props => props.orientation === 'vertical' ? '4px' : '300px'};\n`\n"]}
1
+ {"version":3,"file":"Slider.js","sourceRoot":"/","sources":["Slider.tsx"],"names":[],"mappings":";;;;;;;;;;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B,CAAA;AAED,IAAM,UAAU,GAAG,2BAAM,CAAC,GAAG,6KAE3B,4DAGU,EAA2D,qCAE5D,EAA2D,KACrE,KAHW,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAlD,CAAkD,EAE5D,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAlD,CAAkD,CACrE,CAAA","sourcesContent":["import classNames from 'classnames'\nimport interact from 'interactjs'\nimport React, { CSSProperties, useEffect, useRef, useState } from 'react'\nimport { Rect } from 'spase'\nimport styled, { css, CSSProp } from 'styled-components'\nimport { Orientation } from './types'\n\nconst debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:slider') : () => {}\n\nexport type Props = {\n /**\n * ID attribute of the root element.\n */\n id?: string\n\n /**\n * Class attribute of the root element.\n */\n className?: string\n\n /**\n * Inline style attribute of the root element.\n */\n style?: CSSProperties\n\n /**\n * By default the position is a value from 0 - 1, 0 being the start of the slider and 1 being the\n * end. Switching on this flag inverts this behavior, where 0 becomes the end of the slider and 1\n * being the start.\n */\n isInverted?: boolean\n\n /**\n * Indicates if position change events are dispatched only when dragging ends. When disabled,\n * aforementioned events are fired repeatedly while dragging.\n */\n onlyDispatchesOnDragEnd?: boolean\n\n /**\n * A function that returns the label to be displayed at a given slider position.\n *\n * @param position - The current slider position.\n *\n * @returns The label.\n */\n labelProvider?: (position: number) => string\n\n /**\n * Padding between the gutter and the knob in pixels.\n */\n gutterPadding?: number\n\n /**\n * Height of the knob in pixels.\n */\n knobHeight?: number\n\n /**\n * Width of the knob in pixels.\n */\n knobWidth?: number\n\n /**\n * Orientation of the slider.\n */\n orientation?: Orientation\n\n /**\n * The current position.\n */\n position?: number\n\n /**\n * Handler invoked when position changes.\n *\n * @param position - The current slider position.\n * @param isDragging - Indicates if the position change is triggered by dragging the slider.\n */\n onPositionChange?: (position: number, isDragging: boolean) => void\n\n /**\n * Handler invoked when dragging ends.\n */\n onDragEnd?: () => void\n\n /**\n * Handler invoked when dragging begins.\n */\n onDragStart?: () => void\n\n /**\n * Custom CSS provided to the gutter before the knob.\n */\n startingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the gutter after the knob.\n */\n endingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the knob.\n */\n knobCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the label inside the knob.\n */\n labelCSS?: CSSProp<any>\n}\n\n/**\n * A slider component that divides the scroll gutter into two different elements—one that is before\n * the knob and one that is after the knob. This allows for individual styling customizations. The\n * width and height of the root element of this component is inferred from CSS. The dimension of the\n * knob itself does not impact that of the root element. This component supports both horizontal and\n * vertical orientations.\n */\nexport default function Slider({\n id,\n className,\n style,\n isInverted = false,\n onlyDispatchesOnDragEnd = false,\n gutterPadding = 0,\n knobHeight = 30,\n knobWidth = 30,\n orientation = 'vertical',\n position = 0,\n labelProvider,\n onDragEnd,\n onDragStart,\n onPositionChange,\n startingGutterCSS,\n endingGutterCSS,\n knobCSS,\n labelCSS,\n}: Props) {\n /**\n * Initializes input interactivity of the knob.\n */\n function initInteractivity() {\n const knob = knobRef.current\n if (!knob || interact.isSet(knob)) return\n\n debug('Initializing interactivity...', 'OK')\n\n interact(knob).draggable({\n inertia: true,\n onstart: () => onKnobDragStart(),\n onmove: ({ dx, dy }) => onKnobDragMove(orientation === 'vertical' ? dy : dx),\n onend: () => onKnobDragStop(),\n })\n }\n\n /**\n * Deinitializes input interactivity of the knob.\n */\n function deinitInteractivity() {\n const knob = knobRef.current\n if (!knob || !interact.isSet(knob)) return\n\n debug('Deinitializing interactivity...', 'OK')\n\n interact(knob).unset()\n }\n\n /**\n * Handler invoked when the knob starts dragging. Note that this is an event listener and does not\n * have read access to component states.\n */\n function onKnobDragStart() {\n debug('Handling drag start...', 'OK')\n setIsDragging(true)\n onDragStart?.()\n }\n\n /**\n * Handler invoked when the knob moves. Note that this is an event listener and does not have read\n * access to component states.\n *\n * @param delta - The distance traveled (in pixels) since the last invocation of this handler.\n */\n function onKnobDragMove(delta: number) {\n const rect = Rect.from(rootRef.current) ?? new Rect()\n const naturalPosition = isInverted ? 1 - livePosition.current : livePosition.current\n const naturalNewPositionX = naturalPosition * rect.width + delta\n const naturalNewPositionY = naturalPosition * rect.height + delta\n const naturalNewPosition = (orientation === 'vertical') ? Math.max(0, Math.min(1, naturalNewPositionY / rect.height)) : Math.max(0, Math.min(1, naturalNewPositionX / rect.width))\n const newPosition = isInverted ? 1 - naturalNewPosition : naturalNewPosition\n\n setIsDragging(true)\n setLivePosition(newPosition)\n }\n\n /**\n * Handler invoked when the knob stops dragging. Note that this is an event listener and does not\n * have read access to component states.\n */\n function onKnobDragStop() {\n debug('Handling drag stop...', 'OK')\n setIsDragging(false)\n onDragEnd?.()\n }\n\n /**\n * Sets the current live position. The live position is different from the position state value.\n * Because states are asynchronous by nature, this live position value is used to record position\n * changes when drag event happens. This position should be normalized. That is, inversion should\n * be taken care of prior to passing the new value to this method if `isInverted` is `true`.\n *\n * @param position - The value to set the live position to.\n */\n function setLivePosition(position: number) {\n if (livePosition.current === position) return\n livePosition.current = position\n // debug('Updating live position...', 'OK', position)\n _setPosition(position)\n }\n\n const rootRef = useRef<HTMLDivElement>(null)\n const knobRef = useRef<HTMLButtonElement>(null)\n\n const livePosition = useRef(position)\n\n const [_position, _setPosition] = useState(livePosition.current)\n const [isDragging, setIsDragging] = useState<boolean | undefined>(undefined)\n\n const naturalPosition = isInverted ? 1 - _position : _position\n\n useEffect(() => {\n initInteractivity()\n\n return () => {\n deinitInteractivity()\n }\n }, [])\n\n useEffect(() => {\n if (isDragging === true) return\n if (position === _position) return\n setIsDragging(undefined)\n setLivePosition(position)\n }, [position])\n\n useEffect(() => {\n if (isDragging === true && onlyDispatchesOnDragEnd) return\n onPositionChange?.(_position, isDragging === true)\n }, [_position])\n\n useEffect(() => {\n if (isDragging === false && onlyDispatchesOnDragEnd) {\n onPositionChange?.(_position, false)\n }\n }, [isDragging])\n\n return (\n <StyledRoot ref={rootRef} id={id} className={className} orientation={orientation} style={style}>\n <StyledGutter orientation={orientation} css={startingGutterCSS}\n style={orientation === 'vertical' ? {\n top: 0,\n height: `calc(${naturalPosition*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n left: 0,\n width: `calc(${naturalPosition*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n <StyledKnobContainer ref={knobRef} style={{\n transform: 'translate3d(-50%, -50%, 0)',\n ...(orientation === 'vertical' ? {\n left: '50%',\n top: `${naturalPosition*100}%`,\n transition: isDragging === false ? 'top 100ms ease-out' : 'none',\n } : {\n left: `${naturalPosition*100}%`,\n top: '50%',\n transition: isDragging === false ? 'left 100ms ease-out' : 'none',\n }),\n }}>\n <StyledKnob\n className={classNames({\n 'at-end': isInverted ? (_position === 0) : (_position === 1),\n 'at-start': isInverted ? (_position === 1) : (_position === 0),\n 'dragging': isDragging === true,\n })}\n css={knobCSS}\n style={{\n height: `${knobHeight}px`,\n width: `${knobWidth}px`,\n }}\n >\n {labelProvider && (\n <StyledLabel knobHeight={knobHeight} css={labelCSS}>{labelProvider(_position)}</StyledLabel>\n )}\n </StyledKnob>\n </StyledKnobContainer>\n <StyledGutter orientation={orientation} css={endingGutterCSS}\n style={orientation === 'vertical' ? {\n bottom: 0,\n height: `calc(${(1 - naturalPosition)*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n right: 0,\n width: `calc(${(1 - naturalPosition)*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n </StyledRoot>\n )\n}\n\nconst StyledGutter = styled.div<{ orientation: NonNullable<Props['orientation']> }>`\n background: #fff;\n position: absolute;\n\n ${props => props.orientation === 'vertical' ? css`\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n ` : css`\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n `}\n\n ${props => props.css}\n`\n\nconst StyledLabel = styled.label<{ knobHeight: NonNullable<Props['knobHeight']> }>`\n color: #000;\n font-size: ${props => props.knobHeight * .5}px;\n pointer-events: none;\n user-select: none;\n\n ${props => props.css}\n`\n\nconst StyledKnobContainer = styled.button`\n position: absolute;\n z-index: 1;\n`\n\nconst StyledKnob = styled.div`\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ${props => props.css}\n`\n\nconst StyledRoot = styled.div<{\n orientation: Orientation\n}>`\n box-sizing: border-box;\n display: block;\n height: ${props => props.orientation === 'vertical' ? '300px' : '4px'};\n position: relative;\n width: ${props => props.orientation === 'vertical' ? '4px' : '300px'};\n`\n"]}
@@ -0,0 +1,29 @@
1
+ import { CSSProperties } from 'react';
2
+ import { CSSProp } from 'styled-components';
3
+ import { Orientation } from './types';
4
+ export declare type Props = {
5
+ id?: string;
6
+ className?: string;
7
+ style?: CSSProperties;
8
+ isInverted?: boolean;
9
+ onlyDispatchesOnDragEnd?: boolean;
10
+ labelProvider?: (position: number, index: number) => string;
11
+ gutterPadding?: number;
12
+ knobHeight?: number;
13
+ knobWidth?: number;
14
+ orientation?: Orientation;
15
+ steps?: readonly number[];
16
+ index?: number;
17
+ onIndexChange?: (index: number, isDragging: boolean) => void;
18
+ onPositionChange?: (position: number, isDragging: boolean) => void;
19
+ onDragEnd?: () => void;
20
+ onDragStart?: () => void;
21
+ startingGutterCSS?: CSSProp<any>;
22
+ endingGutterCSS?: CSSProp<any>;
23
+ knobCSS?: CSSProp<any>;
24
+ labelCSS?: CSSProp<any>;
25
+ };
26
+ export declare function generateSteps(length: number): readonly number[];
27
+ export declare function getNearestIndexByPosition(position: number, steps: readonly number[]): number;
28
+ export declare function getPositionAt(index: number, steps: readonly number[]): number;
29
+ export default function StepwiseSlider({ id, className, style, isInverted, onlyDispatchesOnDragEnd, gutterPadding, knobHeight, knobWidth, orientation, labelProvider, onDragEnd, onDragStart, onPositionChange, startingGutterCSS, endingGutterCSS, knobCSS, labelCSS, steps, index, onIndexChange, }: Props): JSX.Element;
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
+ return cooked;
5
+ };
6
+ var __assign = (this && this.__assign) || function () {
7
+ __assign = Object.assign || function(t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
+ t[p] = s[p];
12
+ }
13
+ return t;
14
+ };
15
+ return __assign.apply(this, arguments);
16
+ };
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __read = (this && this.__read) || function (o, n) {
37
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
38
+ if (!m) return o;
39
+ var i = m.call(o), r, ar = [], e;
40
+ try {
41
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
42
+ }
43
+ catch (error) { e = { error: error }; }
44
+ finally {
45
+ try {
46
+ if (r && !r.done && (m = i["return"])) m.call(i);
47
+ }
48
+ finally { if (e) throw e.error; }
49
+ }
50
+ return ar;
51
+ };
52
+ var __importDefault = (this && this.__importDefault) || function (mod) {
53
+ return (mod && mod.__esModule) ? mod : { "default": mod };
54
+ };
55
+ Object.defineProperty(exports, "__esModule", { value: true });
56
+ exports.getPositionAt = exports.getNearestIndexByPosition = exports.generateSteps = void 0;
57
+ var classnames_1 = __importDefault(require("classnames"));
58
+ var interactjs_1 = __importDefault(require("interactjs"));
59
+ var react_1 = __importStar(require("react"));
60
+ var spase_1 = require("spase");
61
+ var styled_components_1 = __importStar(require("styled-components"));
62
+ var debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:stepwise-slider') : function () { };
63
+ function generateSteps(length) {
64
+ if (length <= 1)
65
+ throw new Error('`length` value must be greater than or equal to 2');
66
+ if (Math.round(length) !== length)
67
+ throw new Error('`length` value must be an integer');
68
+ var interval = 1 / (length - 1);
69
+ return Array(length).fill(null).map(function (v, i) {
70
+ var position = interval * i;
71
+ return position;
72
+ });
73
+ }
74
+ exports.generateSteps = generateSteps;
75
+ function getNearestIndexByPosition(position, steps) {
76
+ var index = -1;
77
+ var minDelta = NaN;
78
+ for (var i = 0, n = steps.length; i < n; i++) {
79
+ var step = getPositionAt(i, steps);
80
+ if (isNaN(step))
81
+ continue;
82
+ var delta = Math.abs(position - step);
83
+ if (isNaN(minDelta) || (delta < minDelta)) {
84
+ minDelta = delta;
85
+ index = i;
86
+ }
87
+ }
88
+ return index;
89
+ }
90
+ exports.getNearestIndexByPosition = getNearestIndexByPosition;
91
+ function getPositionAt(index, steps) {
92
+ if (index >= steps.length)
93
+ return NaN;
94
+ return steps[index];
95
+ }
96
+ exports.getPositionAt = getPositionAt;
97
+ function StepwiseSlider(_a) {
98
+ var id = _a.id, className = _a.className, style = _a.style, _b = _a.isInverted, isInverted = _b === void 0 ? false : _b, _c = _a.onlyDispatchesOnDragEnd, onlyDispatchesOnDragEnd = _c === void 0 ? false : _c, _d = _a.gutterPadding, gutterPadding = _d === void 0 ? 0 : _d, _e = _a.knobHeight, knobHeight = _e === void 0 ? 30 : _e, _f = _a.knobWidth, knobWidth = _f === void 0 ? 30 : _f, _g = _a.orientation, orientation = _g === void 0 ? 'vertical' : _g, labelProvider = _a.labelProvider, onDragEnd = _a.onDragEnd, onDragStart = _a.onDragStart, onPositionChange = _a.onPositionChange, startingGutterCSS = _a.startingGutterCSS, endingGutterCSS = _a.endingGutterCSS, knobCSS = _a.knobCSS, labelCSS = _a.labelCSS, _h = _a.steps, steps = _h === void 0 ? generateSteps(10) : _h, _j = _a.index, index = _j === void 0 ? 0 : _j, onIndexChange = _a.onIndexChange;
99
+ function initInteractivity() {
100
+ var knob = knobRef.current;
101
+ if (!knob || interactjs_1.default.isSet(knob))
102
+ return;
103
+ debug('Initializing interactivity...', 'OK');
104
+ (0, interactjs_1.default)(knob).draggable({
105
+ inertia: true,
106
+ onstart: function () { return onKnobDragStart(); },
107
+ onmove: function (_a) {
108
+ var dx = _a.dx, dy = _a.dy;
109
+ return onKnobDragMove(orientation === 'vertical' ? dy : dx);
110
+ },
111
+ onend: function () { return onKnobDragStop(); },
112
+ });
113
+ }
114
+ function deinitInteractivity() {
115
+ var knob = knobRef.current;
116
+ if (!knob || !interactjs_1.default.isSet(knob))
117
+ return;
118
+ debug('Deinitializing interactivity...', 'OK');
119
+ (0, interactjs_1.default)(knob).unset();
120
+ }
121
+ function onKnobDragStart() {
122
+ debug('Handling drag start...', 'OK');
123
+ setIsDragging(true);
124
+ onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart();
125
+ }
126
+ function onKnobDragMove(delta) {
127
+ var _a;
128
+ var rect = (_a = spase_1.Rect.from(rootRef.current)) !== null && _a !== void 0 ? _a : new spase_1.Rect();
129
+ var naturalPosition = isInverted ? 1 - livePosition.current : livePosition.current;
130
+ var naturalNewPositionX = naturalPosition * rect.width + delta;
131
+ var naturalNewPositionY = naturalPosition * rect.height + delta;
132
+ var naturalNewPosition = (orientation === 'vertical') ? Math.max(0, Math.min(1, naturalNewPositionY / rect.height)) : Math.max(0, Math.min(1, naturalNewPositionX / rect.width));
133
+ var newPosition = isInverted ? 1 - naturalNewPosition : naturalNewPosition;
134
+ var newIndex = getNearestIndexByPosition(newPosition, steps);
135
+ setIsDragging(true);
136
+ setLivePosition(newPosition);
137
+ setLiveIndex(newIndex);
138
+ }
139
+ function onKnobDragStop() {
140
+ debug('Handling drag stop...', 'OK');
141
+ setIsDragging(false);
142
+ onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd();
143
+ }
144
+ function setLivePosition(position) {
145
+ if (livePosition.current === position)
146
+ return;
147
+ livePosition.current = position;
148
+ _setPosition(position);
149
+ }
150
+ function setLiveIndex(index) {
151
+ if (liveIndex.current === index)
152
+ return;
153
+ liveIndex.current = index;
154
+ _setIndex(index);
155
+ }
156
+ var rootRef = (0, react_1.useRef)(null);
157
+ var knobRef = (0, react_1.useRef)(null);
158
+ var livePosition = (0, react_1.useRef)(getPositionAt(index, steps));
159
+ var liveIndex = (0, react_1.useRef)(index);
160
+ var _k = __read((0, react_1.useState)(livePosition.current), 2), _position = _k[0], _setPosition = _k[1];
161
+ var _l = __read((0, react_1.useState)(liveIndex.current), 2), _index = _l[0], _setIndex = _l[1];
162
+ var _m = __read((0, react_1.useState)(undefined), 2), isDragging = _m[0], setIsDragging = _m[1];
163
+ var naturalPosition = isInverted ? 1 - _position : _position;
164
+ (0, react_1.useEffect)(function () {
165
+ initInteractivity();
166
+ return function () {
167
+ deinitInteractivity();
168
+ };
169
+ }, []);
170
+ (0, react_1.useEffect)(function () {
171
+ if (isDragging === true)
172
+ return;
173
+ if (index === _index)
174
+ return;
175
+ setIsDragging(undefined);
176
+ setLivePosition(getPositionAt(index, steps));
177
+ setLiveIndex(index);
178
+ }, [index]);
179
+ (0, react_1.useEffect)(function () {
180
+ if (isDragging === true && onlyDispatchesOnDragEnd)
181
+ return;
182
+ onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange(_position, isDragging !== undefined);
183
+ }, [_position]);
184
+ (0, react_1.useEffect)(function () {
185
+ if (isDragging === true && onlyDispatchesOnDragEnd)
186
+ return;
187
+ onIndexChange === null || onIndexChange === void 0 ? void 0 : onIndexChange(_index, isDragging !== undefined);
188
+ }, [_index]);
189
+ (0, react_1.useEffect)(function () {
190
+ if (isDragging !== false)
191
+ return;
192
+ var nearestIndex = getNearestIndexByPosition(_position, steps);
193
+ var nearestPosition = getPositionAt(nearestIndex, steps);
194
+ setLivePosition(nearestPosition);
195
+ if (onlyDispatchesOnDragEnd) {
196
+ onIndexChange === null || onIndexChange === void 0 ? void 0 : onIndexChange(_index, true);
197
+ }
198
+ }, [isDragging]);
199
+ return (react_1.default.createElement(StyledRoot, { ref: rootRef, id: id, className: className, orientation: orientation, style: style },
200
+ react_1.default.createElement(StyledGutter, { orientation: orientation, css: startingGutterCSS, style: orientation === 'vertical' ? {
201
+ top: 0,
202
+ height: "calc(".concat(naturalPosition * 100, "% - ").concat(knobHeight * .5, "px - ").concat(gutterPadding, "px)"),
203
+ } : {
204
+ left: 0,
205
+ width: "calc(".concat(naturalPosition * 100, "% - ").concat(knobWidth * .5, "px - ").concat(gutterPadding, "px)"),
206
+ } }),
207
+ react_1.default.createElement(StyledKnobContainer, { ref: knobRef, style: __assign({ transform: 'translate3d(-50%, -50%, 0)' }, (orientation === 'vertical' ? {
208
+ left: '50%',
209
+ top: "".concat(naturalPosition * 100, "%"),
210
+ transition: isDragging === false ? 'top 100ms ease-out' : 'none',
211
+ } : {
212
+ left: "".concat(naturalPosition * 100, "%"),
213
+ top: '50%',
214
+ transition: isDragging === false ? 'left 100ms ease-out' : 'none',
215
+ })) },
216
+ react_1.default.createElement(StyledKnob, { className: (0, classnames_1.default)({
217
+ 'at-end': isInverted ? (_position === 0) : (_position === 1),
218
+ 'at-start': isInverted ? (_position === 1) : (_position === 0),
219
+ 'dragging': isDragging === true,
220
+ }), css: knobCSS, style: {
221
+ height: "".concat(knobHeight, "px"),
222
+ width: "".concat(knobWidth, "px"),
223
+ } }, steps && labelProvider && (react_1.default.createElement(StyledLabel, { knobHeight: knobHeight, css: labelCSS }, labelProvider(_position, getNearestIndexByPosition(_position, steps)))))),
224
+ react_1.default.createElement(StyledGutter, { orientation: orientation, css: endingGutterCSS, style: orientation === 'vertical' ? {
225
+ bottom: 0,
226
+ height: "calc(".concat((1 - naturalPosition) * 100, "% - ").concat(knobHeight * .5, "px - ").concat(gutterPadding, "px)"),
227
+ } : {
228
+ right: 0,
229
+ width: "calc(".concat((1 - naturalPosition) * 100, "% - ").concat(knobWidth * .5, "px - ").concat(gutterPadding, "px)"),
230
+ } })));
231
+ }
232
+ exports.default = StepwiseSlider;
233
+ var StyledGutter = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n background: #fff;\n position: absolute;\n\n ", "\n\n ", "\n"], ["\n background: #fff;\n position: absolute;\n\n ", "\n\n ", "\n"])), function (props) { return props.orientation === 'vertical' ? (0, styled_components_1.css)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "], ["\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n "]))) : (0, styled_components_1.css)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "], ["\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n "]))); }, function (props) { return props.css; });
234
+ var StyledLabel = styled_components_1.default.label(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n\n ", "\n"], ["\n color: #000;\n font-size: ", "px;\n pointer-events: none;\n user-select: none;\n\n ", "\n"])), function (props) { return props.knobHeight * .5; }, function (props) { return props.css; });
235
+ var StyledKnobContainer = styled_components_1.default.button(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n position: absolute;\n z-index: 1;\n"], ["\n position: absolute;\n z-index: 1;\n"])));
236
+ var StyledKnob = styled_components_1.default.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ", "\n"], ["\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ", "\n"])), function (props) { return props.css; });
237
+ var StyledRoot = styled_components_1.default.div(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"], ["\n box-sizing: border-box;\n display: block;\n height: ", ";\n position: relative;\n width: ", ";\n"])), function (props) { return props.orientation === 'vertical' ? '300px' : '4px'; }, function (props) { return props.orientation === 'vertical' ? '4px' : '300px'; });
238
+ var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7;
239
+ //# sourceMappingURL=StepwiseSlider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StepwiseSlider.js","sourceRoot":"/","sources":["StepwiseSlider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0DAAmC;AACnC,0DAAiC;AACjC,6CAAyE;AACzE,+BAA4B;AAC5B,qEAAwD;AAGxD,IAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,cAAO,CAAC,C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CAAC,CAAC,CAAC,OAAO,EAAlD,CAAkD,CACrE,CAAA","sourcesContent":["import classNames from 'classnames'\nimport interact from 'interactjs'\nimport React, { CSSProperties, useEffect, useRef, useState } from 'react'\nimport { Rect } from 'spase'\nimport styled, { css, CSSProp } from 'styled-components'\nimport { Orientation } from './types'\n\nconst debug = process.env.NODE_ENV === 'development' ? require('debug')('etudes:stepwise-slider') : () => {}\n\nexport type Props = {\n /**\n * ID attribute of the root element.\n */\n id?: string\n\n /**\n * Class attribute of the root element.\n */\n className?: string\n\n /**\n * Inline style attribute of the root element.\n */\n style?: CSSProperties\n\n /**\n * By default the position is a value from 0 - 1, 0 being the start of the slider and 1 being the\n * end. Switching on this flag inverts this behavior, where 0 becomes the end of the slider and 1\n * being the start.\n */\n isInverted?: boolean\n\n /**\n * Indicates if position/index change events are dispatched only when dragging ends. When\n * disabled, aforementioned events are fired repeatedly while dragging.\n */\n onlyDispatchesOnDragEnd?: boolean\n\n /**\n * A function that returns the label to be displayed at a given slider position and closest step\n * index (if steps are provided).\n *\n * @param position - The current slider position.\n * @param index - The nearest step index (if steps are provided), or -1 if no steps are provided.\n *\n * @returns The label.\n */\n labelProvider?: (position: number, index: number) => string\n\n /**\n * Padding between the gutter and the knob in pixels.\n */\n gutterPadding?: number\n\n /**\n * Height of the knob in pixels.\n */\n knobHeight?: number\n\n /**\n * Width of the knob in pixels.\n */\n knobWidth?: number\n\n /**\n * Orientation of the slider.\n */\n orientation?: Orientation\n\n /**\n * An array of step descriptors. A step is a position (0 - 1 inclusive) on the gutter where the\n * knob should snap to if dragging stops near it. Ensure that there are at least two steps: one\n * for the start of the gutter and one for the end.\n */\n steps?: readonly number[]\n\n /**\n * The current index.\n */\n index?: number\n\n /**\n * Handler invoked when index changes. This happens simultaneously with `onPositionChange`.\n *\n * @param index - The current step index.\n * @param isDragging - Indicates if the index change is triggered by dragging the slider..\n */\n onIndexChange?: (index: number, isDragging: boolean) => void\n\n /**\n * Handler invoked when position changes.\n *\n * @param position - The current slider position.\n * @param isDragging - Indicates if the position change is triggered by dragging the slider.\n */\n onPositionChange?: (position: number, isDragging: boolean) => void\n\n /**\n * Handler invoked when dragging ends.\n */\n onDragEnd?: () => void\n\n /**\n * Handler invoked when dragging begins.\n */\n onDragStart?: () => void\n\n /**\n * Custom CSS provided to the gutter before the knob.\n */\n startingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the gutter after the knob.\n */\n endingGutterCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the knob.\n */\n knobCSS?: CSSProp<any>\n\n /**\n * Custom CSS provided to the label inside the knob.\n */\n labelCSS?: CSSProp<any>\n}\n\n/**\n * Generates a set of steps compatible with this component.\n *\n * @param length - The number of steps. This must be at least 2 because you must include the\n * starting and ending points.\n *\n * @returns An array of steps.\n */\nexport function generateSteps(length: number): readonly number[] {\n if (length <= 1) throw new Error('`length` value must be greater than or equal to 2')\n if (Math.round(length) !== length) throw new Error('`length` value must be an integer')\n\n const interval = 1 / (length - 1)\n\n return Array(length).fill(null).map((v, i) => {\n const position = interval * i\n return position\n })\n}\n\n/**\n * Gets the index of the step of which the specified position is closest to. If for whatever\n * reason the index cannot be computed, -1 is returned.\n *\n * @param position - The position (0 - 1, inclusive).\n * @param steps - The steps.\n *\n * @returns The nearest index.\n */\nexport function getNearestIndexByPosition(position: number, steps: readonly number[]): number {\n let index = -1\n let minDelta = NaN\n\n for (let i = 0, n = steps.length; i < n; i++) {\n const step = getPositionAt(i, steps)\n\n if (isNaN(step)) continue\n\n const delta = Math.abs(position - step)\n\n if (isNaN(minDelta) || (delta < minDelta)) {\n minDelta = delta\n index = i\n }\n }\n\n return index\n}\n\n/**\n * Gets the position by step index. This value ranges between 0 - 1, inclusive.\n *\n * @param index - The step index.\n * @param steps - The steps.\n *\n * @returns The position. If for whatever reason the position cannot be determined, `NaN` is\n * returned.\n */\nexport function getPositionAt(index: number, steps: readonly number[]): number {\n if (index >= steps.length) return NaN\n return steps[index]\n}\n\n/**\n * A \"stepwise\" slider component that automatically snaps to certain points on the slider. The\n * component divides the scroll gutter into two different elements—one that is before the knob and\n * one that is after the knob. This allows for individual styling customizations. The width and\n * height of the root element of this component is inferred from its CSS rules. The dimension of the\n * knob itself does not impact that of the root element. This component supports both horizontal and\n * vertical orientations.\n */\nexport default function StepwiseSlider({\n id,\n className,\n style,\n isInverted = false,\n onlyDispatchesOnDragEnd = false,\n gutterPadding = 0,\n knobHeight = 30,\n knobWidth = 30,\n orientation = 'vertical',\n labelProvider,\n onDragEnd,\n onDragStart,\n onPositionChange,\n startingGutterCSS,\n endingGutterCSS,\n knobCSS,\n labelCSS,\n steps = generateSteps(10),\n index = 0,\n onIndexChange,\n}: Props) {\n /**\n * Initializes input interactivity of the knob.\n */\n function initInteractivity() {\n const knob = knobRef.current\n if (!knob || interact.isSet(knob)) return\n\n debug('Initializing interactivity...', 'OK')\n\n interact(knob).draggable({\n inertia: true,\n onstart: () => onKnobDragStart(),\n onmove: ({ dx, dy }) => onKnobDragMove(orientation === 'vertical' ? dy : dx),\n onend: () => onKnobDragStop(),\n })\n }\n\n /**\n * Deinitializes input interactivity of the knob.\n */\n function deinitInteractivity() {\n const knob = knobRef.current\n if (!knob || !interact.isSet(knob)) return\n\n debug('Deinitializing interactivity...', 'OK')\n\n interact(knob).unset()\n }\n\n /**\n * Handler invoked when the knob starts dragging. Note that this is an event listener and does not\n * have read access to component states.\n */\n function onKnobDragStart() {\n debug('Handling drag start...', 'OK')\n setIsDragging(true)\n onDragStart?.()\n }\n\n /**\n * Handler invoked when the knob moves. Note that this is an event listener and does not have read\n * access to component states.\n *\n * @param delta - The distance traveled (in pixels) since the last invocation of this handler.\n */\n function onKnobDragMove(delta: number) {\n const rect = Rect.from(rootRef.current) ?? new Rect()\n const naturalPosition = isInverted ? 1 - livePosition.current : livePosition.current\n const naturalNewPositionX = naturalPosition * rect.width + delta\n const naturalNewPositionY = naturalPosition * rect.height + delta\n const naturalNewPosition = (orientation === 'vertical') ? Math.max(0, Math.min(1, naturalNewPositionY / rect.height)) : Math.max(0, Math.min(1, naturalNewPositionX / rect.width))\n const newPosition = isInverted ? 1 - naturalNewPosition : naturalNewPosition\n const newIndex = getNearestIndexByPosition(newPosition, steps)\n\n setIsDragging(true)\n setLivePosition(newPosition)\n setLiveIndex(newIndex)\n }\n\n /**\n * Handler invoked when the knob stops dragging. Note that this is an event listener and does not\n * have read access to component states.\n */\n function onKnobDragStop() {\n debug('Handling drag stop...', 'OK')\n setIsDragging(false)\n onDragEnd?.()\n }\n\n /**\n * Sets the current live position. The live position is different from the position state value.\n * Because states are asynchronous by nature, this live position value is used to record position\n * changes when drag event happens. This position should be normalized. That is, inversion should\n * be taken care of prior to passing the new value to this method if `isInverted` is `true`.\n *\n * @param position - The value to set the live position to.\n */\n function setLivePosition(position: number) {\n if (livePosition.current === position) return\n livePosition.current = position\n // debug('Updating live position...', 'OK', position)\n _setPosition(position)\n }\n\n /**\n * Sets the current live index. The live index is different from the index state value. Because\n * states are asynchronous by nature, this live index value is used to record index changes when\n * drag event happens.\n *\n * @param index - The value to set the live index to.\n */\n function setLiveIndex(index: number) {\n if (liveIndex.current === index) return\n liveIndex.current = index\n // debug('Updating live index...', 'OK', index)\n _setIndex(index)\n }\n\n const rootRef = useRef<HTMLDivElement>(null)\n const knobRef = useRef<HTMLButtonElement>(null)\n\n const livePosition = useRef(getPositionAt(index, steps))\n const liveIndex = useRef(index)\n\n const [_position, _setPosition] = useState(livePosition.current)\n const [_index, _setIndex] = useState(liveIndex.current)\n const [isDragging, setIsDragging] = useState<boolean | undefined>(undefined)\n\n const naturalPosition = isInverted ? 1 - _position : _position\n\n useEffect(() => {\n initInteractivity()\n\n return () => {\n deinitInteractivity()\n }\n }, [])\n\n useEffect(() => {\n if (isDragging === true) return\n if (index === _index) return\n setIsDragging(undefined)\n setLivePosition(getPositionAt(index, steps))\n setLiveIndex(index)\n }, [index])\n\n useEffect(() => {\n if (isDragging === true && onlyDispatchesOnDragEnd) return\n onPositionChange?.(_position, isDragging !== undefined)\n }, [_position])\n\n useEffect(() => {\n if (isDragging === true && onlyDispatchesOnDragEnd) return\n onIndexChange?.(_index, isDragging !== undefined)\n }, [_index])\n\n useEffect(() => {\n if (isDragging !== false) return\n\n const nearestIndex = getNearestIndexByPosition(_position, steps)\n const nearestPosition = getPositionAt(nearestIndex, steps)\n setLivePosition(nearestPosition)\n\n if (onlyDispatchesOnDragEnd) {\n onIndexChange?.(_index, true)\n }\n }, [isDragging])\n\n return (\n <StyledRoot ref={rootRef} id={id} className={className} orientation={orientation} style={style}>\n <StyledGutter orientation={orientation} css={startingGutterCSS}\n style={orientation === 'vertical' ? {\n top: 0,\n height: `calc(${naturalPosition*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n left: 0,\n width: `calc(${naturalPosition*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n <StyledKnobContainer ref={knobRef} style={{\n transform: 'translate3d(-50%, -50%, 0)',\n ...(orientation === 'vertical' ? {\n left: '50%',\n top: `${naturalPosition*100}%`,\n transition: isDragging === false ? 'top 100ms ease-out' : 'none',\n } : {\n left: `${naturalPosition*100}%`,\n top: '50%',\n transition: isDragging === false ? 'left 100ms ease-out' : 'none',\n }),\n }}>\n <StyledKnob\n className={classNames({\n 'at-end': isInverted ? (_position === 0) : (_position === 1),\n 'at-start': isInverted ? (_position === 1) : (_position === 0),\n 'dragging': isDragging === true,\n })}\n css={knobCSS}\n style={{\n height: `${knobHeight}px`,\n width: `${knobWidth}px`,\n }}\n >\n {steps && labelProvider && (\n <StyledLabel knobHeight={knobHeight} css={labelCSS}>{labelProvider(_position, getNearestIndexByPosition(_position, steps))}</StyledLabel>\n )}\n </StyledKnob>\n </StyledKnobContainer>\n <StyledGutter orientation={orientation} css={endingGutterCSS}\n style={orientation === 'vertical' ? {\n bottom: 0,\n height: `calc(${(1 - naturalPosition)*100}% - ${knobHeight*.5}px - ${gutterPadding}px)`,\n } : {\n right: 0,\n width: `calc(${(1 - naturalPosition)*100}% - ${knobWidth*.5}px - ${gutterPadding}px)`,\n }}\n />\n </StyledRoot>\n )\n}\n\nconst StyledGutter = styled.div<{ orientation: NonNullable<Props['orientation']> }>`\n background: #fff;\n position: absolute;\n\n ${props => props.orientation === 'vertical' ? css`\n left: 0;\n margin: 0 auto;\n right: 0;\n width: 100%;\n ` : css`\n bottom: 0;\n height: 100%;\n margin: auto 0;\n top: 0;\n `}\n\n ${props => props.css}\n`\n\nconst StyledLabel = styled.label<{ knobHeight: NonNullable<Props['knobHeight']> }>`\n color: #000;\n font-size: ${props => props.knobHeight * .5}px;\n pointer-events: none;\n user-select: none;\n\n ${props => props.css}\n`\n\nconst StyledKnobContainer = styled.button`\n position: absolute;\n z-index: 1;\n`\n\nconst StyledKnob = styled.div`\n align-items: center;\n background: #fff;\n box-sizing: border-box;\n display: flex;\n flex-direction: column;\n justify-content: center;\n opacity: 1;\n touch-action: none;\n transition-duration: 100ms;\n transition-property: background, color, opacity, transform;\n transition-timing-function: ease-out;\n\n ${props => props.css}\n`\n\nconst StyledRoot = styled.div<{\n orientation: Orientation\n}>`\n box-sizing: border-box;\n display: block;\n height: ${props => props.orientation === 'vertical' ? '300px' : '4px'};\n position: relative;\n width: ${props => props.orientation === 'vertical' ? '4px' : '300px'};\n`\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etudes",
3
- "version": "0.29.0",
3
+ "version": "0.34.0",
4
4
  "description": "A study of styled React components",
5
5
  "main": "lib",
6
6
  "scripts": {