Add mouseMode prop for selecting items by click or drag#83
Open
krlls wants to merge 1 commit intoadcentury:mainfrom
Open
Add mouseMode prop for selecting items by click or drag#83krlls wants to merge 1 commit intoadcentury:mainfrom
krlls wants to merge 1 commit intoadcentury:mainfrom
Conversation
- Added the `mouseMode` prop to control item selection behavior. - If set to 'click', item selection will happen by clicking. - If set to 'drag', item selection will happen by dragging (similar to mobile behavior).
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
I am still facing this issue. Is this merged? |
Owner
|
Hi, @krlls , thanks for the PR. I don't see the use cases for this feature right now. If you use this component in "mobile mode" (you can toggle it using chrome devtool), you can drag n drop the item to select it. This is how it was designed to behave. So IMO there is already a "drag" mode. |
|
바쁜사람을 위한 javascript import { jsxs as q, jsx as I } from 'react/jsx-runtime'
import {
createContext as L,
useMemo as u,
useReducer as nt,
useCallback as l,
useContext as H,
useState as O,
useEffect as A,
useRef as y,
} from 'react'
const rt = 216,
ot = 36,
ct = 'off',
U = L(null)
U.displayName = 'PickerDataContext'
function Y(c) {
const r = H(U)
if (r === null) {
const e = new Error(`<${c} /> is missing a parent <Picker /> component.`)
throw (Error.captureStackTrace && Error.captureStackTrace(e, Y), e)
}
return r
}
const F = L(null)
F.displayName = 'PickerActionsContext'
function G(c) {
const r = H(F)
if (r === null) {
const e = new Error(`<${c} /> is missing a parent <Picker /> component.`)
throw (Error.captureStackTrace && Error.captureStackTrace(e, G), e)
}
return r
}
function it(c, r = e => e) {
return c.slice().sort((e, i) => {
const o = r(e),
n = r(i)
if (o === null || n === null) return 0
const s = o.compareDocumentPosition(n)
return s & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : s & Node.DOCUMENT_POSITION_PRECEDING ? 1 : 0
})
}
function st(c, r) {
switch (r.type) {
case 'REGISTER_OPTION': {
const { key: e, option: i } = r
let o = [...(c[e] || []), i]
return (
(o = it(o, n => n.element.current)),
{
...c,
[e]: o,
}
)
}
case 'UNREGISTER_OPTION': {
const { key: e, option: i } = r
return {
...c,
[e]: (c[e] || []).filter(o => o !== i),
}
}
default:
throw Error(`Unknown action: ${r.type}`)
}
}
function at(c) {
const {
style: r,
children: e,
value: i,
onChange: o,
height: n = rt,
itemHeight: s = ot,
wheelMode: d = ct,
...p
} = c,
v = u(
() => ({
height: s,
marginTop: -(s / 2),
position: 'absolute',
top: '50%',
left: 0,
width: '100%',
pointerEvents: 'none',
}),
[s],
),
h = u(
() => ({
height: `${n}px`,
position: 'relative',
display: 'flex',
justifyContent: 'center',
overflow: 'hidden',
maskImage:
'linear-gradient(to top, transparent, transparent 5%, white 20%, white 80%, transparent 95%, transparent)',
WebkitMaskImage:
'linear-gradient(to top, transparent, transparent 5%, white 20%, white 80%, transparent 95%, transparent)',
}),
[n],
),
[x, g] = nt(st, {}),
T = u(() => ({ height: n, itemHeight: s, wheelMode: d, value: i, optionGroups: x }), [n, s, i, x, d]),
f = l(
(E, m) => {
if (i[E] === m) return !1
const C = { ...i, [E]: m }
return (o(C, E), !0)
},
[o, i],
),
P = l(
(E, m) => (
g({ type: 'REGISTER_OPTION', key: E, option: m }),
() => g({ type: 'UNREGISTER_OPTION', key: E, option: m })
),
[],
),
w = u(() => ({ registerOption: P, change: f }), [P, f])
return q('div', {
style: {
...h,
...r,
},
...p,
children: [
I(F.Provider, {
value: w,
children: I(U.Provider, { value: T, children: e }),
}),
q('div', {
style: v,
children: [
I('div', {
style: {
position: 'absolute',
top: 0,
bottom: 'auto',
left: 0,
right: 'auto',
width: '100%',
height: '1px',
background: '#d9d9d9',
transform: 'scaleY(0.5)',
},
}),
I('div', {
style: {
position: 'absolute',
top: 'auto',
bottom: 0,
left: 0,
right: 'auto',
width: '100%',
height: '1px',
background: '#d9d9d9',
transform: 'scaleY(0.5)',
},
}),
],
}),
],
})
}
const W = L(null)
W.displayName = 'PickerColumnDataContext'
function J(c) {
const r = H(W)
if (r === null) {
const e = new Error(`<${c} /> is missing a parent <Picker.Column /> component.`)
throw (Error.captureStackTrace && Error.captureStackTrace(e, J), e)
}
return r
}
function lt({ style: c, children: r, name: e, ...i }) {
const { height: o, itemHeight: n, wheelMode: s, value: d, optionGroups: p } = Y('Picker.Column'),
v = u(() => d[e], [d, e]),
h = u(() => p[e] || [], [e, p]),
x = u(() => {
let t = h.findIndex(a => a.value === v)
return (t < 0 && (t = 0), t)
}, [h, v]),
g = u(() => o / 2 - n * h.length + n / 2, [o, n, h]),
T = u(() => o / 2 - n / 2, [o, n]),
[f, P] = O(0)
A(() => {
P(o / 2 - n / 2 - x * n)
}, [o, n, x])
const w = G('Picker.Column'),
E = y(f)
E.current = f
const m = l(() => {
let t = 0
const a = E.current
;(a >= T ? (t = 0) : a <= g ? (t = h.length - 1) : (t = -Math.round((a - T) / n)),
w.change(e, h[t].value) || P(o / 2 - n / 2 - t * n))
}, [w, o, n, e, T, g, h]),
[C, N] = O(0),
[k, M] = O(!1),
[$, R] = O(0),
[isDragging, setIsDragging] = O(false),
[hasDragged, setHasDragged] = O(false),
S = l(
t => {
;(t < g ? (t = g - Math.pow(g - t, 0.8)) : t > T && (t = T + Math.pow(t - T, 0.8)), P(t))
},
[T, g],
),
Q = l(
t => {
;(R(t.targetTouches[0].pageY), N(f), setIsDragging(true), setHasDragged(false))
},
[f],
),
b = l(
t => {
;(t.cancelable && t.preventDefault(), k || M(!0), setHasDragged(true))
const a = C + t.targetTouches[0].pageY - $
S(a)
},
[k, C, $, S],
),
X = l(() => {
if (k) {
M(!1)
R(0)
N(0)
m()
}
setIsDragging(false)
setTimeout(() => setHasDragged(false), 100)
}, [m, k]),
Z = l(() => {
if (k) {
M(!1)
R(0)
P(C)
N(0)
}
setIsDragging(false)
setTimeout(() => setHasDragged(false), 100)
}, [k, C]),
handleMouseDown = l(
t => {
R(t.pageY)
N(f)
setIsDragging(true)
setHasDragged(false)
},
[f],
),
handleMouseMove = l(
t => {
if (!isDragging) return
t.preventDefault()
k || M(!0)
setHasDragged(true)
const a = C + t.pageY - $
S(a)
},
[isDragging, k, C, $, S],
),
handleMouseUp = l(() => {
if (isDragging) {
if (k) {
M(!1)
R(0)
N(0)
m()
}
setIsDragging(false)
setTimeout(() => setHasDragged(false), 100)
}
}, [isDragging, k, m]),
handleMouseLeave = l(() => {
if (isDragging) {
if (k) {
M(!1)
R(0)
P(C)
N(0)
}
setIsDragging(false)
setTimeout(() => setHasDragged(false), 100)
}
}, [isDragging, k, C]),
D = y(null),
j = l(
t => {
if (t.deltaY === 0) return
let a = t.deltaY * 0.1
;(Math.abs(a) < n && (a = n * Math.sign(a)), s === 'normal' && (a = -a))
a = -a
const B = f + a
S(B)
},
[n, f, S, s],
),
z = l(() => {
m()
}, [m]),
_ = l(
t => {
s !== 'off' &&
(t.cancelable && t.preventDefault(),
j(t),
D.current && clearTimeout(D.current),
(D.current = setTimeout(() => {
z()
}, 200)))
},
[z, j, D, s],
),
V = y(null)
A(() => {
const t = V.current
return (
t && (t.addEventListener('touchmove', b, { passive: !1 }), t.addEventListener('wheel', _, { passive: !1 })),
() => {
t && (t.removeEventListener('touchmove', b), t.removeEventListener('wheel', _))
}
)
}, [b, _])
A(() => {
if (isDragging) {
document.addEventListener('mousemove', handleMouseMove)
document.addEventListener('mouseup', handleMouseUp)
return () => {
document.removeEventListener('mousemove', handleMouseMove)
document.removeEventListener('mouseup', handleMouseUp)
}
}
}, [isDragging, handleMouseMove, handleMouseUp])
const tt = u(
() => ({
flex: '1 1 0%',
maxHeight: '100%',
transitionProperty: 'transform',
transitionTimingFunction: 'cubic-bezier(0, 0, 0.2, 1)',
transitionDuration: k ? '0ms' : '300ms',
transform: `translate3d(0, ${f}px, 0)`,
cursor: isDragging ? 'grabbing' : 'grab',
userSelect: 'none',
WebkitUserSelect: 'none',
}),
[f, k, isDragging],
),
et = u(() => ({ key: e, hasDragged }), [e, hasDragged])
return I('div', {
style: {
...tt,
...c,
},
ref: V,
onTouchStart: Q,
onTouchEnd: X,
onTouchCancel: Z,
onMouseDown: handleMouseDown,
...i,
children: I(W.Provider, { value: et, children: r }),
})
}
function ut(c) {
return typeof c == 'function'
}
function ht({ style: c, children: r, value: e, ...i }) {
const o = y(null),
{ itemHeight: n, value: s } = Y('Picker.Item'),
d = G('Picker.Item'),
{ key: p, hasDragged } = J('Picker.Item')
A(() => d.registerOption(p, { value: e, element: o }), [p, d, e])
const v = u(
() => ({
height: `${n}px`,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
cursor: 'pointer',
}),
[n],
),
h = l(
event => {
if (hasDragged) {
return
}
d.change(p, e)
},
[d, p, e, hasDragged],
)
return I('div', {
style: {
...v,
...c,
},
ref: o,
onClick: h,
...i,
children: ut(r) ? r({ selected: s[p] === e }) : r,
})
}
const K = at
K.Column = lt
K.Item = ht
export { K as default } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi!
I’ve added a
mouseModeprop that allows controlling the method of item selection with a mouse. Now, you can select an item either by clicking or by dragging, depending on the set value.mouseModeprop with values 'click' and 'drag'.