File tree Expand file tree Collapse file tree 3 files changed +30
-15
lines changed
packages/vue-primitives/src/label Expand file tree Collapse file tree 3 files changed +30
-15
lines changed Original file line number Diff line number Diff line change @@ -7,3 +7,25 @@ export interface LabelProps {
77export type LabelEmits = {
88 mousedown : [ event : MouseEvent ]
99}
10+
11+ export interface UseLabelEmits {
12+ onMousedown ?: ( event : MouseEvent ) => void
13+ }
14+
15+ export function useLabel ( emits ?: UseLabelEmits ) {
16+ function onMousedown ( event : MouseEvent ) {
17+ // only prevent text selection if clicking inside the label itself
18+ const target = event . target as HTMLElement
19+ if ( target . closest ( 'button, input, select, textarea' ) )
20+ return
21+
22+ emits ?. onMousedown ?.( event )
23+ // prevent text selection when double clicking label
24+ if ( ! event . defaultPrevented && event . detail > 1 )
25+ event . preventDefault ( )
26+ }
27+
28+ return ( ) => ( {
29+ onMousedown,
30+ } )
31+ }
Original file line number Diff line number Diff line change @@ -11,24 +11,15 @@ withDefaults(defineProps<LabelProps>(), {
1111})
1212const emit = defineEmits <LabelEmits >()
1313
14- function onMousedown(event : MouseEvent ) {
15- // only prevent text selection if clicking inside the label itself
16- const target = event .target as HTMLElement
17- if (target .closest (' button, input, select, textarea' ))
18- return
19-
20- emit (' mousedown' , event )
21- // prevent text selection when double clicking label
22- if (! event .defaultPrevented && event .detail > 1 )
23- event .preventDefault ()
24- }
14+ const label = useLabel ({
15+ onMousedown(event ) {
16+ emit (' mousedown' , event )
17+ },
18+ })
2519 </script >
2620
2721<template >
28- <Primitive
29- :as =" as"
30- @mousedown =" onMousedown"
31- >
22+ <Primitive :as =" as" v-bind =" label" >
3223 <slot />
3324 </Primitive >
3425</template >
Original file line number Diff line number Diff line change 11export {
22 type LabelEmits ,
33 type LabelProps ,
4+ useLabel ,
5+ type UseLabelEmits ,
46} from './Label.ts'
57
68export { default as Label } from './Label.vue'
You can’t perform that action at this time.
0 commit comments