Skip to content

Commit 23138b8

Browse files
committed
feat: new InjectStyleFeature feature to inject style elements
1 parent 4f9a6ca commit 23138b8

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

packages/webshell/etc/webshell.api.md

+8
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ export interface HTMLDimensions {
275275
// @public
276276
export type HTMLDimensionsImplementation = 'resize' | 'mutation' | 'polling';
277277

278+
// @public
279+
export const InjectStyleFeature: FeatureClass<InjectStyleFeatureOptions>;
280+
281+
// @public
282+
export interface InjectStyleFeatureOptions {
283+
css: string;
284+
}
285+
278286
// @public
279287
export interface LinkPressOptions {
280288
ignoreHashChange?: boolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import injectStyleScript from './InjectStyleFeature.webjs';
2+
import FeatureBuilder from '../FeatureBuilder';
3+
import type { FeatureClass } from '../Feature';
4+
5+
/**
6+
* An object describing options for the inject style feature.
7+
*
8+
* @public
9+
*/
10+
export interface InjectStyleFeatureOptions {
11+
/**
12+
* CSS markup to inject in a `<style>` element.
13+
*/
14+
css: string;
15+
}
16+
17+
/**
18+
* This feature injects a `<style>` element in the header of the page.
19+
*
20+
* @public
21+
*/
22+
export const InjectStyleFeature: FeatureClass<InjectStyleFeatureOptions> = new FeatureBuilder<InjectStyleFeatureOptions>(
23+
{
24+
defaultOptions: { css: '' },
25+
identifier: '@native-html/iframe-strip-body-spacing',
26+
script: injectStyleScript
27+
}
28+
).build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function InjectStyleFeature(context) {
2+
var options = context.options || {},
3+
css = options.css || '',
4+
head = document.head || document.getElementsByTagName('head')[0],
5+
style = document.createElement('style');
6+
style.type = 'text/css';
7+
style.appendChild(document.createTextNode(css));
8+
head.appendChild(style);
9+
}

packages/webshell/src/features/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './HandleHashChangeFeature';
55
export * from './HandleVisualViewportFeature';
66
export * from './ForceResponsiveViewportFeature';
77
export * from './ForceElementSizeFeature';
8+
export * from './InjectStyleFeature';

0 commit comments

Comments
 (0)