UNPKG

4.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var DEFAULT_HEIGHT = '50%';
4var DEFAULT_WIDTH = 20;
5/**
6 * - Generates a style used to fade out an overflowing content by defining a style for an :after pseudo element.
7 * - Apply it to the :after selector for all combination of states the parent of content might have (normal, hover,
8 * selected, focus).
9 * - Requires the target to have position set to relative and overflow set to hidden.
10 *
11 * @example
12 * ```tsx
13 * // Assuming the following DOM structure and the different background colors coming from
14 * // the parent holding the content.
15 * <div className={classNames.parent}>
16 * <span className={classNames.content}>Overflown Content</span>
17 * </div>
18 * ```
19 * ```ts
20 * // This is how the style set would look in Component.styles.ts
21 * const { bodyBackground } = theme.semanticColors;
22 * const { neutralLighter } = theme.palette;
23 *
24 * // The second argument of getFadedOverflowStyle function is a string representing a key of
25 * // ISemanticColors or IPalette.
26 *
27 * const styles = {
28 * parent: [
29 * backgroundColor: bodyBackground,
30 * selectors: {
31 * '&:hover: {
32 * backgroundColor: neutralLighter
33 * },
34 * '$content:after': {
35 * ...getFadedOverflowStyle(theme, 'bodyBackground')
36 * },
37 * '&:hover $content:after': {
38 * ...getFadedOverflowStyle(theme, 'neutralLighter')
39 * }
40 * }
41 * ],
42 * content: [
43 * width: '100%',
44 * display: 'inline-block',
45 * position: 'relative',
46 * overflow: 'hidden'
47 * ]
48 * }
49 * ```
50 * @param theme - The theme object to use.
51 * @param color - The background color to fade out to. Accepts only keys of ISemanticColors or IPalette.
52 * Defaults to 'bodyBackground'.
53 * @param direction - The direction of the overflow. Defaults to horizontal.
54 * @param width - The width of the fading overflow. Vertical direction defaults it to 100% vs 20px when horizontal.
55 * @param height - The Height of the fading overflow. Vertical direction defaults it to 50% vs 100% when horizontal.
56 * @returns The style object.
57 */
58function getFadedOverflowStyle(theme, color, direction, width, height) {
59 if (color === void 0) { color = 'bodyBackground'; }
60 if (direction === void 0) { direction = 'horizontal'; }
61 if (width === void 0) { width = getDefaultValue('width', direction); }
62 if (height === void 0) { height = getDefaultValue('height', direction); }
63 // Get the color value string from the theme semanticColors or palette.
64 var colorValue = theme.semanticColors[color] || theme.palette[color];
65 // Get the red, green, blue values of the colorValue.
66 var rgbColor = color2rgb(colorValue);
67 // Apply opacity 0 to serve as a start color of the gradient.
68 var rgba = "rgba(" + rgbColor.r + ", " + rgbColor.g + ", " + rgbColor.b + ", 0)";
69 // Get the direction of the gradient. (mergeStyles takes care of RTL direction)
70 var gradientDirection = direction === 'vertical' ? 'to bottom' : 'to right';
71 return {
72 content: '""',
73 position: 'absolute',
74 right: 0,
75 bottom: 0,
76 width: width,
77 height: height,
78 pointerEvents: 'none',
79 backgroundImage: "linear-gradient(" + gradientDirection + ", " + rgba + " 0%, " + colorValue + " 100%)",
80 };
81}
82exports.getFadedOverflowStyle = getFadedOverflowStyle;
83// TODO consider moving this to a separate module along with some more color functions from OUFR/utilities.
84/**
85 * Helper function to convert a string hex color to an RGB object.
86 *
87 * @param colorValue - Color to be converted from hex to rgba.
88 */
89function color2rgb(colorValue) {
90 if (colorValue[0] === '#') {
91 // If it's a hex code
92 return {
93 r: parseInt(colorValue.slice(1, 3), 16),
94 g: parseInt(colorValue.slice(3, 5), 16),
95 b: parseInt(colorValue.slice(5, 7), 16),
96 };
97 }
98 else if (colorValue.indexOf('rgba(') === 0) {
99 // If it's an rgba color string
100 colorValue = colorValue.match(/rgba\(([^)]+)\)/)[1];
101 var parts = colorValue.split(/ *, */).map(Number);
102 return {
103 r: parts[0],
104 g: parts[1],
105 b: parts[2],
106 };
107 }
108 // The only remaining possibility is transparent.
109 return {
110 r: 255,
111 g: 255,
112 b: 255,
113 };
114}
115/**
116 * Helper function to get the default values for parameters of main function.
117 *
118 * @param style - Which style to get the default value for.
119 * @param direction - What direction to take into consideration.
120 */
121function getDefaultValue(style, direction) {
122 if (style === 'width') {
123 return direction === 'horizontal' ? DEFAULT_WIDTH : '100%';
124 }
125 else {
126 return direction === 'vertical' ? DEFAULT_HEIGHT : '100%';
127 }
128}
129//# sourceMappingURL=getFadedOverflowStyle.js.map
\No newline at end of file