UNPKG

4.39 kBTypeScriptView Raw
1import { IStyle } from './IStyle';
2export declare const InjectionMode: {
3 /**
4 * Avoids style injection, use getRules() to read the styles.
5 */
6 none: 0;
7 /**
8 * Inserts rules using the insertRule api.
9 */
10 insertNode: 1;
11 /**
12 * Appends rules using appendChild.
13 */
14 appendChild: 2;
15};
16export declare type InjectionMode = typeof InjectionMode[keyof typeof InjectionMode];
17/**
18 * CSP settings for the stylesheet
19 */
20export interface ICSPSettings {
21 /**
22 * Nonce to inject into script tag
23 */
24 nonce?: string;
25}
26/**
27 * Stylesheet config.
28 *
29 * @public
30 */
31export interface IStyleSheetConfig {
32 /**
33 * Injection mode for how rules are inserted.
34 */
35 injectionMode?: InjectionMode;
36 /**
37 * Default 'displayName' to use for a className.
38 * @defaultvalue 'css'
39 */
40 defaultPrefix?: string;
41 /**
42 * Defines the default direction of rules for auto-rtlifying things.
43 * While typically this is represented as a DIR attribute in the markup,
44 * the DIR is not enough to control whether padding goes on the left or
45 * right. Use this to set the default direction when rules are registered.
46 */
47 rtl?: boolean;
48 /**
49 * Default 'namespace' to attach before the className.
50 */
51 namespace?: string;
52 /**
53 * CSP settings
54 */
55 cspSettings?: ICSPSettings;
56 /**
57 * Callback executed when a rule is inserted.
58 */
59 onInsertRule?: (rule: string) => void;
60 /**
61 * Initial value for classnames cache. Key is serialized css rules associated with a classname.
62 */
63 classNameCache?: {
64 [key: string]: string;
65 };
66}
67/**
68 * Represents the state of styles registered in the page. Abstracts
69 * the surface for adding styles to the stylesheet, exposes helpers
70 * for reading the styles registered in server rendered scenarios.
71 *
72 * @public
73 */
74export declare class Stylesheet {
75 private _lastStyleElement?;
76 private _styleElement?;
77 private _rules;
78 private _preservedRules;
79 private _config;
80 private _rulesToInsert;
81 private _counter;
82 private _keyToClassName;
83 private _onResetCallbacks;
84 private _classNameToArgs;
85 /**
86 * Gets the singleton instance.
87 */
88 static getInstance(): Stylesheet;
89 constructor(config?: IStyleSheetConfig);
90 /**
91 * Configures the stylesheet.
92 */
93 setConfig(config?: IStyleSheetConfig): void;
94 /**
95 * Configures a reset callback.
96 *
97 * @param callback - A callback which will be called when the Stylesheet is reset.
98 */
99 onReset(callback: () => void): void;
100 /**
101 * Generates a unique classname.
102 *
103 * @param displayName - Optional value to use as a prefix.
104 */
105 getClassName(displayName?: string): string;
106 /**
107 * Used internally to cache information about a class which was
108 * registered with the stylesheet.
109 */
110 cacheClassName(className: string, key: string, args: IStyle[], rules: string[]): void;
111 /**
112 * Gets the appropriate classname given a key which was previously
113 * registered using cacheClassName.
114 */
115 classNameFromKey(key: string): string | undefined;
116 /**
117 * Gets all classnames cache with the stylesheet.
118 */
119 getClassNameCache(): {
120 [key: string]: string;
121 };
122 /**
123 * Gets the arguments associated with a given classname which was
124 * previously registered using cacheClassName.
125 */
126 argsFromClassName(className: string): IStyle[] | undefined;
127 /**
128 * Gets the arguments associated with a given classname which was
129 * previously registered using cacheClassName.
130 */
131 insertedRulesFromClassName(className: string): string[] | undefined;
132 /**
133 * Inserts a css rule into the stylesheet.
134 * @param preserve - Preserves the rule beyond a reset boundary.
135 */
136 insertRule(rule: string, preserve?: boolean): void;
137 /**
138 * Gets all rules registered with the stylesheet; only valid when
139 * using InsertionMode.none.
140 */
141 getRules(includePreservedRules?: boolean): string;
142 /**
143 * Resets the internal state of the stylesheet. Only used in server
144 * rendered scenarios where we're using InsertionMode.none.
145 */
146 reset(): void;
147 resetKeys(): void;
148 private _getStyleElement;
149 private _createStyleElement;
150 private _findPlaceholderStyleTag;
151}