Skip to content

useScreenOrientation

Category
Export Size
848 B
Last Changed
last week

Reactive Screen Orientation API. It provides web developers with information about the user's current screen orientation.

Demo

For best results, please use a mobile or tablet device (or use your browser's native inspector to simulate an orientation change)
isSupported: true
Orientation Type: landscape-primary
Orientation Angle: 0

Usage

ts
import { 
useScreenOrientation
} from '@vueuse/core'
const {
isSupported
,
orientation
,
angle
,
lockOrientation
,
unlockOrientation
,
} =
useScreenOrientation
()

To lock the orientation, you can pass an OrientationLockType to the lockOrientation function:

ts
import { 
useScreenOrientation
} from '@vueuse/core'
const {
isSupported
,
orientation
,
angle
,
lockOrientation
,
unlockOrientation
,
} =
useScreenOrientation
()
lockOrientation
('portrait-primary')

and then unlock again, with the following:

ts
unlockOrientation
()

Accepted orientation types are one of "landscape-primary", "landscape-secondary", "portrait-primary", "portrait-secondary", "any", "landscape", "natural" and "portrait".

Screen Orientation API MDN

Type Declarations

Show Type Declarations
ts
export type 
OrientationType
=
| "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary" export type
OrientationLockType
=
| "any" | "natural" | "landscape" | "portrait" | "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary" export interface ScreenOrientation extends EventTarget {
lock
: (
orientation
:
OrientationLockType
) =>
Promise
<void>
unlock
: () => void
readonly
type
:
OrientationType
readonly
angle
: number
addEventListener
: (
type
: "change",
listener
: (
this
: this,
ev
: Event) => any,
useCapture
?: boolean,
) => void } /** * Reactive screen orientation * * @see https://vueuse.org/useScreenOrientation * * @__NO_SIDE_EFFECTS__ */ export declare function
useScreenOrientation
(
options
?:
ConfigurableWindow
): {
isSupported
:
ComputedRef
<boolean>
orientation
:
Ref
<
OrientationType
| undefined,
OrientationType
| undefined>
angle
:
ShallowRef
<number, number>
lockOrientation
: (
type
:
OrientationLockType
) =>
Promise
<void>
unlockOrientation
: () => void
} export type
UseScreenOrientationReturn
=
ReturnType
<typeof
useScreenOrientation
>

Source

SourceDemoDocs

Contributors

Anthony Fu
SerKo
Anthony Fu
Michael J. Roberts
Robin
IlyaL
Fernando Fernández
Satrong
JunaYa
Jelf
Nicolas Hedger

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)
v10.8.0 on
a086e - fix: stricter types
v10.7.0 on
be3cc - fix: Add fault tolerance to lock and unlock (#3575)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access