@react-aria/slider
Advanced tools
Comparing version 3.0.0-alpha.0 to 3.0.0-nightly.2350
@@ -29,18 +29,2 @@ var { | ||
function $dec1906781d9c7cb69245fc4d5344b$export$computeOffsetToValue(offset, props, trackRef) { | ||
const { | ||
minValue = 0, | ||
maxValue = 100, | ||
step = 1 | ||
} = props; | ||
if (!trackRef.current) { | ||
return minValue; | ||
} | ||
const containerSize = trackRef.current.offsetWidth; | ||
const val = offset / containerSize * (maxValue - minValue) + minValue; | ||
return Math.round((val - minValue) / step) * step + minValue; | ||
} | ||
/** | ||
@@ -62,3 +46,4 @@ * Provides behavior and accessibility for a slider component. | ||
fieldProps | ||
} = useLabel(props); // Attach id of the label to the state so it can be accessed by useSliderThumb. | ||
} = useLabel(props); | ||
const isSliderEditable = !(props.isDisabled || props.isReadOnly); // Attach id of the label to the state so it can be accessed by useSliderThumb. | ||
@@ -71,3 +56,8 @@ $dec1906781d9c7cb69245fc4d5344b$export$sliderIds.set(state, (_labelProps$id = labelProps.id) != null ? _labelProps$id : fieldProps.id); // When the user clicks or drags the track, we want the motion to set and drag the | ||
const realTimeTrackDraggingIndex = useRef(undefined); | ||
const draggableProps = useDrag1D({ | ||
const isTrackDragging = useRef(false); | ||
const { | ||
onMouseDown, | ||
onMouseEnter, | ||
onMouseOut | ||
} = useDrag1D({ | ||
containerRef: trackRef, | ||
@@ -80,6 +70,16 @@ reverse: false, | ||
} | ||
isTrackDragging.current = dragging; | ||
}, | ||
onPositionChange: position => { | ||
if (realTimeTrackDraggingIndex.current !== undefined) { | ||
state.setThumbValue(realTimeTrackDraggingIndex.current, $dec1906781d9c7cb69245fc4d5344b$export$computeOffsetToValue(position, props, trackRef)); | ||
if (realTimeTrackDraggingIndex.current !== undefined && trackRef.current) { | ||
const percent = position / trackRef.current.offsetWidth; | ||
state.setThumbPercent(realTimeTrackDraggingIndex.current, percent); // When track-dragging ends, onDrag is called before a final onPositionChange is | ||
// called, so we can't reset realTimeTrackDraggingIndex until onPositionChange, | ||
// as we still needed to update the thumb position one last time. Hence we | ||
// track whether we're dragging, and the actual dragged index, separately. | ||
if (!isTrackDragging.current) { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
@@ -99,3 +99,3 @@ } | ||
// We only trigger track-dragging if the user clicks on the track itself. | ||
if (trackRef.current && trackRef.current === e.target) { | ||
if (trackRef.current && isSliderEditable) { | ||
// Find the closest thumb | ||
@@ -106,7 +106,8 @@ const trackPosition = trackRef.current.getBoundingClientRect().left; | ||
const percent = offset / trackRef.current.offsetWidth; | ||
const value = state.getPercentValue(percent); | ||
const minDiff = Math.min(...state.values.map(v => Math.abs(v - value))); | ||
const value = state.getPercentValue(percent); // Only compute the diff for thumbs that are editable, as only they can be dragged | ||
const minDiff = Math.min(...state.values.map((v, index) => state.isThumbEditable(index) ? Math.abs(v - value) : Number.POSITIVE_INFINITY)); | ||
const index = state.values.findIndex(v => Math.abs(v - value) === minDiff); | ||
if (index >= 0) { | ||
if (minDiff !== Number.POSITIVE_INFINITY && index >= 0) { | ||
// Don't unfocus anything | ||
@@ -124,6 +125,12 @@ e.preventDefault(); | ||
state.setThumbValue(index, value); | ||
} else { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
} | ||
}, draggableProps) | ||
}, { | ||
onMouseDown, | ||
onMouseEnter, | ||
onMouseOut | ||
}) | ||
}; | ||
@@ -160,3 +167,3 @@ } | ||
const value = state.values[index]; | ||
const allowDrag = !(isDisabled || isReadOnly); | ||
const isEditable = !(isDisabled || isReadOnly); | ||
const focusInput = useCallback(() => { | ||
@@ -185,3 +192,5 @@ if (inputRef.current) { | ||
} | ||
}); | ||
}); // Immediately register editability with the state | ||
state.setThumbEditable(index, isEditable); | ||
const { | ||
@@ -200,3 +209,3 @@ focusableProps | ||
type: 'range', | ||
tabIndex: allowDrag ? 0 : undefined, | ||
tabIndex: isEditable ? 0 : undefined, | ||
min: state.getThumbMinValue(index), | ||
@@ -217,3 +226,3 @@ max: state.getThumbMaxValue(index), | ||
}), | ||
thumbProps: allowDrag ? mergeProps({ | ||
thumbProps: isEditable ? mergeProps({ | ||
onMouseDown: draggableProps.onMouseDown, | ||
@@ -220,0 +229,0 @@ onMouseEnter: draggableProps.onMouseEnter, |
@@ -8,18 +8,2 @@ import { useFocusable } from "@react-aria/focus"; | ||
function $d20491ae7743da17cfa841ff6d87$export$computeOffsetToValue(offset, props, trackRef) { | ||
const { | ||
minValue = 0, | ||
maxValue = 100, | ||
step = 1 | ||
} = props; | ||
if (!trackRef.current) { | ||
return minValue; | ||
} | ||
const containerSize = trackRef.current.offsetWidth; | ||
const val = offset / containerSize * (maxValue - minValue) + minValue; | ||
return Math.round((val - minValue) / step) * step + minValue; | ||
} | ||
/** | ||
@@ -41,3 +25,4 @@ * Provides behavior and accessibility for a slider component. | ||
fieldProps | ||
} = useLabel(props); // Attach id of the label to the state so it can be accessed by useSliderThumb. | ||
} = useLabel(props); | ||
const isSliderEditable = !(props.isDisabled || props.isReadOnly); // Attach id of the label to the state so it can be accessed by useSliderThumb. | ||
@@ -50,3 +35,8 @@ $d20491ae7743da17cfa841ff6d87$export$sliderIds.set(state, (_labelProps$id = labelProps.id) != null ? _labelProps$id : fieldProps.id); // When the user clicks or drags the track, we want the motion to set and drag the | ||
const realTimeTrackDraggingIndex = useRef(undefined); | ||
const draggableProps = useDrag1D({ | ||
const isTrackDragging = useRef(false); | ||
const { | ||
onMouseDown, | ||
onMouseEnter, | ||
onMouseOut | ||
} = useDrag1D({ | ||
containerRef: trackRef, | ||
@@ -59,6 +49,16 @@ reverse: false, | ||
} | ||
isTrackDragging.current = dragging; | ||
}, | ||
onPositionChange: position => { | ||
if (realTimeTrackDraggingIndex.current !== undefined) { | ||
state.setThumbValue(realTimeTrackDraggingIndex.current, $d20491ae7743da17cfa841ff6d87$export$computeOffsetToValue(position, props, trackRef)); | ||
if (realTimeTrackDraggingIndex.current !== undefined && trackRef.current) { | ||
const percent = position / trackRef.current.offsetWidth; | ||
state.setThumbPercent(realTimeTrackDraggingIndex.current, percent); // When track-dragging ends, onDrag is called before a final onPositionChange is | ||
// called, so we can't reset realTimeTrackDraggingIndex until onPositionChange, | ||
// as we still needed to update the thumb position one last time. Hence we | ||
// track whether we're dragging, and the actual dragged index, separately. | ||
if (!isTrackDragging.current) { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
@@ -78,3 +78,3 @@ } | ||
// We only trigger track-dragging if the user clicks on the track itself. | ||
if (trackRef.current && trackRef.current === e.target) { | ||
if (trackRef.current && isSliderEditable) { | ||
// Find the closest thumb | ||
@@ -85,7 +85,8 @@ const trackPosition = trackRef.current.getBoundingClientRect().left; | ||
const percent = offset / trackRef.current.offsetWidth; | ||
const value = state.getPercentValue(percent); | ||
const minDiff = Math.min(...state.values.map(v => Math.abs(v - value))); | ||
const value = state.getPercentValue(percent); // Only compute the diff for thumbs that are editable, as only they can be dragged | ||
const minDiff = Math.min(...state.values.map((v, index) => state.isThumbEditable(index) ? Math.abs(v - value) : Number.POSITIVE_INFINITY)); | ||
const index = state.values.findIndex(v => Math.abs(v - value) === minDiff); | ||
if (index >= 0) { | ||
if (minDiff !== Number.POSITIVE_INFINITY && index >= 0) { | ||
// Don't unfocus anything | ||
@@ -103,6 +104,12 @@ e.preventDefault(); | ||
state.setThumbValue(index, value); | ||
} else { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
} | ||
}, draggableProps) | ||
}, { | ||
onMouseDown, | ||
onMouseEnter, | ||
onMouseOut | ||
}) | ||
}; | ||
@@ -137,3 +144,3 @@ } | ||
const value = state.values[index]; | ||
const allowDrag = !(isDisabled || isReadOnly); | ||
const isEditable = !(isDisabled || isReadOnly); | ||
const focusInput = useCallback(() => { | ||
@@ -162,3 +169,5 @@ if (inputRef.current) { | ||
} | ||
}); | ||
}); // Immediately register editability with the state | ||
state.setThumbEditable(index, isEditable); | ||
const { | ||
@@ -177,3 +186,3 @@ focusableProps | ||
type: 'range', | ||
tabIndex: allowDrag ? 0 : undefined, | ||
tabIndex: isEditable ? 0 : undefined, | ||
min: state.getThumbMinValue(index), | ||
@@ -194,3 +203,3 @@ max: state.getThumbMaxValue(index), | ||
}), | ||
thumbProps: allowDrag ? mergeProps({ | ||
thumbProps: isEditable ? mergeProps({ | ||
onMouseDown: draggableProps.onMouseDown, | ||
@@ -197,0 +206,0 @@ onMouseEnter: draggableProps.onMouseEnter, |
{ | ||
"name": "@react-aria/slider", | ||
"version": "3.0.0-alpha.0", | ||
"version": "3.0.0-nightly.2350+9853bacb", | ||
"description": "Slider", | ||
@@ -21,11 +21,11 @@ "license": "Apache-2.0", | ||
"@babel/runtime": "^7.6.2", | ||
"@react-aria/focus": "^3.2.0", | ||
"@react-aria/i18n": "^3.0.2", | ||
"@react-aria/interactions": "^3.2.0", | ||
"@react-aria/label": "^3.0.2", | ||
"@react-aria/utils": "^3.2.0", | ||
"@react-stately/radio": "^3.0.2", | ||
"@react-stately/slider": "3.0.0-alpha.0", | ||
"@react-types/radio": "^3.0.2", | ||
"@react-types/slider": "3.0.0-alpha.0" | ||
"@react-aria/focus": "3.0.0-nightly.672+9853bacb", | ||
"@react-aria/i18n": "3.0.0-nightly.672+9853bacb", | ||
"@react-aria/interactions": "3.0.0-nightly.672+9853bacb", | ||
"@react-aria/label": "3.0.0-nightly.672+9853bacb", | ||
"@react-aria/utils": "3.0.0-nightly.672+9853bacb", | ||
"@react-stately/radio": "3.0.0-nightly.672+9853bacb", | ||
"@react-stately/slider": "3.0.0-nightly.2350+9853bacb", | ||
"@react-types/radio": "3.0.0-nightly.672+9853bacb", | ||
"@react-types/slider": "3.0.0-nightly.2350+9853bacb" | ||
}, | ||
@@ -38,3 +38,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "661f0f2e3b8648a75aae83043267954700059fe0" | ||
"gitHead": "9853bacb98dd37c64faf573e5cb1a6493e2e6f08" | ||
} |
@@ -13,5 +13,5 @@ /* | ||
import {computeOffsetToValue, sliderIds} from './utils'; | ||
import {HTMLAttributes, useRef} from 'react'; | ||
import {mergeProps, useDrag1D} from '@react-aria/utils'; | ||
import {sliderIds} from './utils'; | ||
import {SliderProps} from '@react-types/slider'; | ||
@@ -49,2 +49,4 @@ import {SliderState} from '@react-stately/slider'; | ||
const isSliderEditable = !(props.isDisabled || props.isReadOnly); | ||
// Attach id of the label to the state so it can be accessed by useSliderThumb. | ||
@@ -58,3 +60,4 @@ sliderIds.set(state, labelProps.id ?? fieldProps.id); | ||
const realTimeTrackDraggingIndex = useRef<number | undefined>(undefined); | ||
const draggableProps = useDrag1D({ | ||
const isTrackDragging = useRef(false); | ||
const {onMouseDown, onMouseEnter, onMouseOut} = useDrag1D({ | ||
containerRef: trackRef as any, | ||
@@ -67,6 +70,16 @@ reverse: false, | ||
} | ||
isTrackDragging.current = dragging; | ||
}, | ||
onPositionChange: (position) => { | ||
if (realTimeTrackDraggingIndex.current !== undefined) { | ||
state.setThumbValue(realTimeTrackDraggingIndex.current, computeOffsetToValue(position, props, trackRef)); | ||
if (realTimeTrackDraggingIndex.current !== undefined && trackRef.current) { | ||
const percent = position / trackRef.current.offsetWidth; | ||
state.setThumbPercent(realTimeTrackDraggingIndex.current, percent); | ||
// When track-dragging ends, onDrag is called before a final onPositionChange is | ||
// called, so we can't reset realTimeTrackDraggingIndex until onPositionChange, | ||
// as we still needed to update the thumb position one last time. Hence we | ||
// track whether we're dragging, and the actual dragged index, separately. | ||
if (!isTrackDragging.current) { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
@@ -89,3 +102,3 @@ } | ||
// We only trigger track-dragging if the user clicks on the track itself. | ||
if (trackRef.current && trackRef.current === e.target) { | ||
if (trackRef.current && isSliderEditable) { | ||
// Find the closest thumb | ||
@@ -97,5 +110,7 @@ const trackPosition = trackRef.current.getBoundingClientRect().left; | ||
const value = state.getPercentValue(percent); | ||
const minDiff = Math.min(...state.values.map(v => Math.abs(v - value))); | ||
// Only compute the diff for thumbs that are editable, as only they can be dragged | ||
const minDiff = Math.min(...state.values.map((v, index) => state.isThumbEditable(index) ? Math.abs(v - value) : Number.POSITIVE_INFINITY)); | ||
const index = state.values.findIndex(v => Math.abs(v - value) === minDiff); | ||
if (index >= 0) { | ||
if (minDiff !== Number.POSITIVE_INFINITY && index >= 0) { | ||
// Don't unfocus anything | ||
@@ -115,7 +130,11 @@ e.preventDefault(); | ||
state.setThumbValue(index, value); | ||
} else { | ||
realTimeTrackDraggingIndex.current = undefined; | ||
} | ||
} | ||
} | ||
}, draggableProps) | ||
}, { | ||
onMouseDown, onMouseEnter, onMouseOut | ||
}) | ||
}; | ||
} |
@@ -52,3 +52,3 @@ import {ChangeEvent, HTMLAttributes, useCallback, useEffect} from 'react'; | ||
const value = state.values[index]; | ||
const allowDrag = !(isDisabled || isReadOnly); | ||
const isEditable = !(isDisabled || isReadOnly); | ||
@@ -83,2 +83,5 @@ const focusInput = useCallback(() => { | ||
// Immediately register editability with the state | ||
state.setThumbEditable(index, isEditable); | ||
const {focusableProps} = useFocusable( | ||
@@ -99,3 +102,3 @@ mergeProps(opts, { | ||
type: 'range', | ||
tabIndex: allowDrag ? 0 : undefined, | ||
tabIndex: isEditable ? 0 : undefined, | ||
min: state.getThumbMinValue(index), | ||
@@ -116,3 +119,3 @@ max: state.getThumbMaxValue(index), | ||
}), | ||
thumbProps: allowDrag ? mergeProps({ | ||
thumbProps: isEditable ? mergeProps({ | ||
onMouseDown: draggableProps.onMouseDown, | ||
@@ -119,0 +122,0 @@ onMouseEnter: draggableProps.onMouseEnter, |
@@ -1,15 +0,3 @@ | ||
import {BaseSliderProps} from '@react-types/slider'; | ||
import {SliderState} from '@react-stately/slider'; | ||
export const sliderIds = new WeakMap<SliderState, string>(); | ||
export function computeOffsetToValue(offset: number, props: BaseSliderProps, trackRef: React.RefObject<HTMLElement>) { | ||
const {minValue = 0, maxValue = 100, step = 1} = props; | ||
if (!trackRef.current) { | ||
return minValue; | ||
} | ||
const containerSize = trackRef.current.offsetWidth; | ||
const val = offset / containerSize * (maxValue - minValue) + minValue; | ||
return Math.round((val - minValue) / step) * step + minValue; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76793
684
1
- Removed@formatjs/ecma402-abstract@2.2.3(transitive)
- Removed@formatjs/fast-memoize@2.2.3(transitive)
- Removed@formatjs/icu-messageformat-parser@2.9.3(transitive)
- Removed@formatjs/icu-skeleton-parser@1.8.7(transitive)
- Removed@formatjs/intl-localematcher@0.5.7(transitive)
- Removed@internationalized/date@3.5.6(transitive)
- Removed@internationalized/message@3.1.5(transitive)
- Removed@internationalized/number@3.5.4(transitive)
- Removed@internationalized/string@3.2.4(transitive)
- Removed@react-aria/focus@3.18.4(transitive)
- Removed@react-aria/i18n@3.12.3(transitive)
- Removed@react-aria/interactions@3.22.4(transitive)
- Removed@react-aria/label@3.7.12(transitive)
- Removed@react-aria/ssr@3.9.6(transitive)
- Removed@react-aria/utils@3.25.3(transitive)
- Removed@react-stately/form@3.0.6(transitive)
- Removed@react-stately/radio@3.10.8(transitive)
- Removed@react-stately/slider@3.0.0-alpha.0(transitive)
- Removed@react-stately/utils@3.10.4(transitive)
- Removed@react-types/radio@3.8.4(transitive)
- Removed@react-types/shared@3.25.0(transitive)
- Removed@react-types/slider@3.0.0-alpha.0(transitive)
- Removed@swc/helpers@0.5.15(transitive)
- Removedclsx@2.1.1(transitive)
- Removedintl-messageformat@10.7.6(transitive)
- Removedtslib@2.8.1(transitive)