Skip to content

Commit be9d795

Browse files
committed
add isMobile and isTouchDevice helpers
1 parent 5042445 commit be9d795

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,29 @@ import { useEngine } from 'use-ua-parser-js';
103103
const myComponent: FC<Props> = (props) => {
104104
const engine = useEngine(); //{ name: string, version: string }
105105
}
106+
```
107+
108+
## Helpers
109+
110+
### `isMobile(device: UAParser.IResult['device']): boolean`
111+
112+
```javascript
113+
import { useDevice, isMobile } from 'use-ua-parser-js';
114+
115+
const myComponent: FC<Props> = (props) => {
116+
const device = useDevice(); //{ model: string, type: string, vendor: string }
117+
const isCurrentDeviceMobile = isMobile(device);
118+
}
119+
```
120+
121+
### `isTouchDevice(device: UAParser.IResult['device']): boolean`
122+
Check if device is either a `mobile`, `tablet` or `wearable` device. Doesn't include "2:1" laptops.
123+
124+
```javascript
125+
import { useDevice, isTouchDevice } from 'use-ua-parser-js';
126+
127+
const myComponent: FC<Props> = (props) => {
128+
const device = useDevice(); //{ model: string, type: string, vendor: string }
129+
const hasTouchScreen = isTouchDevice(device);
130+
}
106131
```

src/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as UAParser from 'ua-parser-js';
2+
3+
function isMobile(details: UAParser.IResult['device']): boolean {
4+
return details.type === 'mobile';
5+
}
6+
7+
function isTouchDevice(details: UAParser.IResult['device']): boolean {
8+
return ['mobile', 'tablet', 'wearable'].includes(details.type || '');
9+
}
10+
11+
export { isMobile, isTouchDevice };

src/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import * as UAParser from 'ua-parser-js';
3+
import { isMobile, isTouchDevice } from './helpers';
34

45
type IUseUAReturn = Omit<UAParser.IResult, 'ua'>;
56

@@ -20,7 +21,7 @@ function useUA(uastring = defaultUAString) {
2021
engine: uaParser.getEngine(),
2122
};
2223
} catch (err) {
23-
return null
24+
return null;
2425
}
2526
}, [uastring]);
2627
}
@@ -31,7 +32,7 @@ function useDevice(uastring = defaultUAString) {
3132
uaParser.setUA(uastring);
3233
return uaParser.getDevice();
3334
} catch (err) {
34-
return null
35+
return null;
3536
}
3637
}, [uastring]);
3738
}
@@ -42,7 +43,7 @@ function useBrowser(uastring = defaultUAString) {
4243
uaParser.setUA(uastring);
4344
return uaParser.getBrowser();
4445
} catch (err) {
45-
return null
46+
return null;
4647
}
4748
}, [uastring]);
4849
}
@@ -53,7 +54,7 @@ function useCPU(uastring = defaultUAString) {
5354
uaParser.setUA(uastring);
5455
return uaParser.getCPU();
5556
} catch (err) {
56-
return null
57+
return null;
5758
}
5859
}, [uastring]);
5960
}
@@ -64,9 +65,9 @@ function useEngine(uastring = defaultUAString) {
6465
uaParser.setUA(uastring);
6566
return uaParser.getEngine();
6667
} catch (err) {
67-
return null
68+
return null;
6869
}
6970
}, [uastring]);
7071
}
7172

72-
export { useUA, useDevice, useBrowser, useCPU, useEngine };
73+
export { useUA, useDevice, useBrowser, useCPU, useEngine, isMobile, isTouchDevice };

0 commit comments

Comments
 (0)