@empathyco/x-components
Version:
Empathy X Components
140 lines (112 loc) • 3.73 kB
Markdown
---
title: DeviceDetector
---
# DeviceDetector
This component helps to detect or setting a device, that can be used later to create
different layouts optimized for different devices. This detected device is available under
the XComponentAliasAPI.device property.
## Props
| Name | Description | Type | Default |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | ----------------------- |
| <code>breakpoints</code> | Record of the device name, that can be whatever you want `xs`, `mobile`, `big`... And<br />the max width in pixels for that device. | <code>Record<Device, MaxWidth></code> | <code>() => ({})</code> |
| <code>force</code> | Forces a device, ignoring the breakpoints prop. | <code>Device</code> | <code></code> |
| <code>throttleMs</code> | Time in milliseconds to throttle the resize events used to detect the device. | <code>number</code> | <code>100</code> |
## Events
This component emits the following events:
- [`DeviceProvided`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts)
## See it in action
This component renders no element to the DOM, but serves as way to safely detect or set the device
name given an object containing all the possible breakpoints.
_Try resizing the browser window!_
```vue live
<template>
<div>
<DeviceDetector :breakpoints="breakpoints" />
{{ $x.device }}
</div>
</template>
<script>
import { DeviceDetector } from '@empathyco/x-components/device'
export default {
name: 'DeviceDemo',
components: {
DeviceDetector,
},
data() {
return {
breakpoints: {
mobile: 600,
tablet: 900,
desktop: Number.POSITIVE_INFINITY,
},
}
},
}
</script>
```
### Play with props
In this example, the `DeviceDetector` has been forced to always detect the `mobile` device. No
matter what the window width is.
_Try resizing the window to check that it never changes_
```vue live
<template>
<div>
<DeviceDetector force="mobile" :breakpoints="breakpoints" />
{{ $x.device }}
</div>
</template>
<script>
import { DeviceDetector } from '@empathyco/x-components/device'
export default {
name: 'DeviceDemo',
components: {
DeviceDetector,
},
data() {
return {
breakpoints: {
mobile: 600,
tablet: 900,
desktop: Number.POSITIVE_INFINITY,
},
}
},
}
</script>
```
### Play with events
In this example, the `DeviceDetector` will emit a `DeviceProvided` event, with the new device as the
payload. This device is stored in a data variable and then displayed.
_Try resizing the browser window!_
```vue live
<template>
<div>
<DeviceDetector :breakpoints="breakpoints" @DeviceProvided="storeDevice" />
{{ device }}
</div>
</template>
<script>
import { DeviceDetector } from '@empathyco/x-components/device'
export default {
name: 'DeviceDemo',
components: {
DeviceDetector,
},
data() {
return {
device: 'unknown',
breakpoints: {
mobile: 600,
tablet: 900,
desktop: Number.POSITIVE_INFINITY,
},
}
},
methods: {
storeDevice(device) {
this.device = device
},
},
}
</script>
```