currentDevice
Get the current device type.
API
currentDevice
Return
| Argument | Description | Type |
|---|---|---|
CurrentDevice | Device type string | 'ipad' | 'android' | 'iphone' | 'pc' |
Parameters
No parameters
Example
Basic Usage
js
import { currentDevice } from 'ranuts';
const device = currentDevice();
console.log(`Current device: ${device}`);
// May output: 'ipad', 'android', 'iphone', or 'pc'Execute Different Logic Based on Device Type
js
import { currentDevice } from 'ranuts';
const device = currentDevice();
switch (device) {
case 'iphone':
// iPhone-specific logic
break;
case 'android':
// Android-specific logic
break;
case 'ipad':
// iPad-specific logic
break;
case 'pc':
// PC-specific logic
break;
}Device-Specific Styles
js
import { currentDevice } from 'ranuts';
const device = currentDevice();
document.body.classList.add(`device-${device}`);Notes
Detection order: Detects in the following order:
- iPad/iPod
- Android
- iPhone
- Others (default returns 'pc')
Server-side rendering: Returns
'pc'in server-side environments (nowindowobject).Detection method: Detects via User Agent string.
Return value: Return value is an enum type, can only be one of
'ipad','android','iphone', or'pc'.