UNPKG

5.57 kBJavaScriptView Raw
1import { HighContrastSelector } from './CommonStyles';
2import { IsFocusVisibleClassName } from '@uifabric/utilities';
3import { ZIndexes } from './zIndexes';
4export function getFocusStyle(theme, insetOrOptions, position, highContrastStyle, borderColor, outlineColor, isFocusedOnly) {
5 if (typeof insetOrOptions === 'number' || !insetOrOptions) {
6 return _getFocusStyleInternal(theme, {
7 inset: insetOrOptions,
8 position: position,
9 highContrastStyle: highContrastStyle,
10 borderColor: borderColor,
11 outlineColor: outlineColor,
12 isFocusedOnly: isFocusedOnly,
13 });
14 }
15 else {
16 return _getFocusStyleInternal(theme, insetOrOptions);
17 }
18}
19function _getFocusStyleInternal(theme, options) {
20 var _a, _b;
21 if (options === void 0) { options = {}; }
22 var _c = options.inset, inset = _c === void 0 ? 0 : _c, _d = options.width, width = _d === void 0 ? 1 : _d, _e = options.position, position = _e === void 0 ? 'relative' : _e, highContrastStyle = options.highContrastStyle, _f = options.borderColor, borderColor = _f === void 0 ? theme.palette.white : _f, _g = options.outlineColor, outlineColor = _g === void 0 ? theme.palette.neutralSecondary : _g, _h = options.isFocusedOnly, isFocusedOnly = _h === void 0 ? true : _h;
23 return {
24 // Clear browser-specific focus styles and use 'transparent' as placeholder for focus style.
25 outline: 'transparent',
26 // Requirement because pseudo-element is absolutely positioned.
27 position: position,
28 selectors: (_a = {
29 // Clear the focus border in Firefox.
30 // Reference: http://stackoverflow.com/a/199319/1436671
31 '::-moz-focus-inner': {
32 border: '0',
33 }
34 },
35 // When the element that uses this mixin is in a :focus state, add a pseudo-element to
36 // create a border.
37 _a["." + IsFocusVisibleClassName + " &" + (isFocusedOnly ? ':focus' : '') + ":after"] = {
38 content: '""',
39 position: 'absolute',
40 left: inset + 1,
41 top: inset + 1,
42 bottom: inset + 1,
43 right: inset + 1,
44 border: width + "px solid " + borderColor,
45 outline: width + "px solid " + outlineColor,
46 zIndex: ZIndexes.FocusStyle,
47 selectors: (_b = {},
48 _b[HighContrastSelector] = highContrastStyle,
49 _b),
50 },
51 _a),
52 };
53}
54/**
55 * Generates style to clear browser specific focus styles.
56 */
57export function focusClear() {
58 return {
59 selectors: {
60 '&::-moz-focus-inner': {
61 // Clear the focus border in Firefox. Reference: http://stackoverflow.com/a/199319/1436671
62 border: 0,
63 },
64 '&': {
65 // Clear browser specific focus styles and use transparent as placeholder for focus style
66 outline: 'transparent',
67 },
68 },
69 };
70}
71/**
72 * Generates a style which can be used to set a border on focus.
73 *
74 * @param theme - The theme object to use.
75 * @param inset - The number of pixels to inset the border (default 0)
76 * @param width - The border width in pixels (default 1)
77 * @param color - Color of the outline (default `theme.palette.neutralSecondary`)
78 * @returns The style object.
79 */
80export function getFocusOutlineStyle(theme, inset, width, color) {
81 var _a;
82 if (inset === void 0) { inset = 0; }
83 if (width === void 0) { width = 1; }
84 return {
85 selectors: (_a = {},
86 _a[":global(" + IsFocusVisibleClassName + ") &:focus"] = {
87 outline: width + " solid " + (color || theme.palette.neutralSecondary),
88 outlineOffset: -inset + "px",
89 },
90 _a),
91 };
92}
93/**
94 * Generates text input border styles on focus.
95 *
96 * @param borderColor - Color of the border.
97 * @param borderRadius - Radius of the border.
98 * @param borderType - Type of the border.
99 * @param borderPosition - Position of the border relative to the input element (default to -1
100 * as it's the most common border width of the input element)
101 * @returns The style object.
102 */
103export var getInputFocusStyle = function (borderColor, borderRadius, borderType, borderPosition) {
104 var _a, _b, _c;
105 if (borderType === void 0) { borderType = 'border'; }
106 if (borderPosition === void 0) { borderPosition = -1; }
107 var isBorderBottom = borderType === 'borderBottom';
108 return {
109 borderColor: borderColor,
110 selectors: {
111 ':after': (_a = {
112 pointerEvents: 'none',
113 content: "''",
114 position: 'absolute',
115 left: isBorderBottom ? 0 : borderPosition,
116 top: borderPosition,
117 bottom: borderPosition,
118 right: isBorderBottom ? 0 : borderPosition
119 },
120 _a[borderType] = "2px solid " + borderColor,
121 _a.borderRadius = borderRadius,
122 _a.width = borderType === 'borderBottom' ? '100%' : undefined,
123 _a.selectors = (_b = {},
124 _b[HighContrastSelector] = (_c = {},
125 _c[borderType === 'border' ? 'borderColor' : 'borderBottomColor'] = 'Highlight',
126 _c),
127 _b),
128 _a),
129 },
130 };
131};
132//# sourceMappingURL=getFocusStyle.js.map
\No newline at end of file