/
use-system-brightness-mode.tsx
59 lines (56 loc) · 1.98 KB
/
use-system-brightness-mode.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React from 'react';
import { Button } from 'react-native-paper';
import { SYSTEM_BRIGHTNESS } from 'expo-permissions';
import { BrightnessMode } from 'expo-brightness';
import { usePermissions, useSystemBrightnessMode } from 'use-expo';
import { Example, Information, Link, Page, Space, MissingPermissions } from '../../atoms';
import { MoleculeProps } from '../../providers/molecule';
import * as url from '../../providers/urls';
export const UseSystemBrightnessMode: React.SFC<MoleculeProps> = (props) => {
const [permission, askPermission] = usePermissions(SYSTEM_BRIGHTNESS);
const [mode, setMode] = useSystemBrightnessMode();
return (
<Page
title={props.name}
subtitle={props.description}
>
<Information>
This example uses both the <Link url={url.docs.permissions}>Permissions</Link> and <Link url={url.docs.brightness}>Brightness</Link> modules.
When you grant the <Link url={url.permissions.systemBrightness}>SYSTEM_BRIGHTNESS</Link> permission, it renders two buttons to switch the system brightness mode.
</Information>
<Example>
{(permission && permission.status !== 'granted') && (
<MissingPermissions onConfirm={askPermission}>
We need permission to modify the system brightness.
</MissingPermissions>
)}
{(permission && permission.status === 'granted') && (
<>
<Space size='small'>
<Button
color='#333'
mode={mode === BrightnessMode.AUTOMATIC ? 'contained' : 'outlined'}
onPress={() => setMode(BrightnessMode.AUTOMATIC)}
>
Automatic
</Button>
</Space>
<Space size='small'>
<Button
color='#333'
mode={mode === BrightnessMode.MANUAL ? 'contained' : 'outlined'}
onPress={() => setMode(BrightnessMode.MANUAL)}
>
Manual
</Button>
</Space>
</>
)}
</Example>
</Page>
);
};
UseSystemBrightnessMode.defaultProps = {
name: 'useSystemBrightnessMode',
description: 'change the system brightness mode',
}