-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathControlBarButton.tsx
More file actions
143 lines (127 loc) · 3.71 KB
/
ControlBarButton.tsx
File metadata and controls
143 lines (127 loc) · 3.71 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import React from 'react';
import { DefaultButton, IButtonProps, IRenderFunction, concatStyleSets, IButtonStyles } from '@fluentui/react';
import { controlButtonStyles } from './styles/ControlBar.styles';
import { ControlButtonTooltip } from './ControlButtonTooltip';
/**
* Strings of {@link ControlBarButton} that can be overridden.
*
* @public
*/
export interface ControlBarButtonStrings {
/**
* Label of the button. This supersedes onLabel or offLabel if used.
*/
label?: string;
/**
* Label of the button shown when the button is checked.
*/
onLabel?: string;
/**
* Label of the button shown when the button is not checked.
*/
offLabel?: string;
/**
* Tooltip content of the button. This supersedes tooltipDisabledContent, tooltipOnContent and tooltipOffContent if used.
*/
tooltipContent?: string;
/**
* Tooltip content when the button is disabled.
*/
tooltipDisabledContent?: string;
/**
* Tooltip content when the button is in the checked state.
*/
tooltipOnContent?: string;
/**
* Tooltip content when the button is in the unchecked state.
*/
tooltipOffContent?: string;
}
/**
* Styles for all {@link ControlBarButton} implementations.
*
* @public
*/
export type ControlBarButtonStyles = IButtonStyles;
/**
* Props for {@link ControlBarButton}.
*
* @public
*/
export interface ControlBarButtonProps extends IButtonProps {
/**
* Whether the label is displayed or not.
*
* @defaultValue `false`
*/
showLabel?: boolean;
/**
* Key to use for the Label component
*/
labelKey?: string;
/**
* Id to use for the tooltip host.
*
* @defaultValue This uses the labelKey and appends -tooltip by default
*/
tooltipId?: string;
/**
* Optional strings to override in component.
*/
strings?: ControlBarButtonStrings;
/**
* Icon to render when the button is checked.
*/
onRenderOnIcon?: IRenderFunction<IButtonProps>;
/**
* Icon to render when the button is not checked.
*/
onRenderOffIcon?: IRenderFunction<IButtonProps>;
/**
* Fluent styles, including extensions common to all {@link ControlBarButton}s.
*/
styles?: ControlBarButtonStyles;
}
const DefaultRenderIcon = (props?: ControlBarButtonProps): JSX.Element | null => {
return props?.checked
? props?.onRenderOnIcon
? props?.onRenderOnIcon()
: null
: props?.onRenderOffIcon
? props?.onRenderOffIcon()
: null;
};
/**
* Default button styled for the {@link ControlBar}.
*
* Use this component create custom buttons that are styled the same as other buttons provided by the UI Library.
*
* @public
*/
export const ControlBarButton = (props: ControlBarButtonProps): JSX.Element => {
const componentStyles = concatStyleSets(controlButtonStyles, props.styles ?? {});
const labelText =
props?.text ?? props?.strings?.label ?? (props?.checked ? props?.strings?.onLabel : props?.strings?.offLabel);
const tooltipContent =
props?.strings?.tooltipContent ??
(props?.disabled
? props?.strings?.tooltipDisabledContent
: props?.checked
? props?.strings?.tooltipOnContent
: props?.strings?.tooltipOffContent);
const tooltipId = props.tooltipId ?? props.labelKey ? props.labelKey + '-tooltip' : undefined;
return (
<ControlButtonTooltip content={tooltipContent} id={tooltipId}>
<DefaultButton
{...props}
styles={componentStyles}
onRenderText={props.showLabel && props.onRenderText ? props.onRenderText : undefined}
onRenderIcon={props.onRenderIcon ?? DefaultRenderIcon}
>
{props.showLabel ? labelText : <></>}
</DefaultButton>
</ControlButtonTooltip>
);
};