Skip to content

useTextSelection

Category
Export Size
786 B
Last Changed
last month

Reactively track user text selection based on Window.getSelection.

Demo

You can select any text on the page.

Selected Text:No selected

Selected rects:

[]

Usage

vue
<script setup lang="ts">
import { 
useTextSelection
} from '@vueuse/core'
const
state
=
useTextSelection
()
</script> <template> <
p
>{{
state
.
text
}}</
p
>
</template>

Type Declarations

Show Type Declarations
ts
/**
 * Reactively track user text selection based on [`Window.getSelection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection).
 *
 * @see https://vueuse.org/useTextSelection
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useTextSelection
(
options
?:
ConfigurableWindow
): {
text
:
ComputedRef
<string>
rects
:
ComputedRef
<DOMRect[]>
ranges
:
ComputedRef
<Range[]>
selection
:
Ref
<
{ readonly
anchorNode
: Node | null
readonly
anchorOffset
: number
readonly
direction
: string
readonly
focusNode
: Node | null
readonly
focusOffset
: number
readonly
isCollapsed
: boolean
readonly
rangeCount
: number
readonly
type
: string
addRange
: (
range
: Range) => void
collapse
: (
node
: Node | null,
offset
?: number) => void
collapseToEnd
: () => void
collapseToStart
: () => void
containsNode
: (
node
: Node,
allowPartialContainment
?: boolean) => boolean
deleteFromDocument
: () => void
empty
: () => void
extend
: (
node
: Node,
offset
?: number) => void
getComposedRanges
: (
options
?: GetComposedRangesOptions) => StaticRange[]
getRangeAt
: (
index
: number) => Range
modify
: (
alter
?: string,
direction
?: string,
granularity
?: string) => void
removeAllRanges
: () => void
removeRange
: (
range
: Range) => void
selectAllChildren
: (
node
: Node) => void
setBaseAndExtent
: (
anchorNode
: Node,
anchorOffset
: number,
focusNode
: Node,
focusOffset
: number,
) => void
setPosition
: (
node
: Node | null,
offset
?: number) => void
toString
: () => string
} | null, | Selection | { readonly
anchorNode
: Node | null
readonly
anchorOffset
: number
readonly
direction
: string
readonly
focusNode
: Node | null
readonly
focusOffset
: number
readonly
isCollapsed
: boolean
readonly
rangeCount
: number
readonly
type
: string
addRange
: (
range
: Range) => void
collapse
: (
node
: Node | null,
offset
?: number) => void
collapseToEnd
: () => void
collapseToStart
: () => void
containsNode
: (
node
: Node,
allowPartialContainment
?: boolean) => boolean
deleteFromDocument
: () => void
empty
: () => void
extend
: (
node
: Node,
offset
?: number) => void
getComposedRanges
: (
options
?: GetComposedRangesOptions) => StaticRange[]
getRangeAt
: (
index
: number) => Range
modify
: (
alter
?: string,
direction
?: string,
granularity
?: string,
) => void
removeAllRanges
: () => void
removeRange
: (
range
: Range) => void
selectAllChildren
: (
node
: Node) => void
setBaseAndExtent
: (
anchorNode
: Node,
anchorOffset
: number,
focusNode
: Node,
focusOffset
: number,
) => void
setPosition
: (
node
: Node | null,
offset
?: number) => void
toString
: () => string
} | null > } export type
UseTextSelectionReturn
=
ReturnType
<typeof
useTextSelection
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
SerKo
Fernando Fernández
Lehoczky Zoltán
wheat
Jelf
webfansplz

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

Join the Biggest FREE AI-Driven Development Event for Vue Developers
Save My Seat