Skip to content

Commit

Permalink
feat: new DOM feature ForceElementSizeFeature
Browse files Browse the repository at this point in the history
This feature will force a specific element to a peculiar size.
  • Loading branch information
jsamr committed Sep 25, 2020
1 parent 2a067e6 commit f715b5f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
71 changes: 71 additions & 0 deletions packages/core/src/features/ForceElementSizeFeature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import script from './ForceElementSizeFeature.webjs';
import { FeatureBuilder } from '../FeatureBuilder';
import type { DOMElementRequest } from '../types';
import type { FeatureConstructor } from '../Feature';

/**
* An object describing customization for the force body feature.
*
* @public
*/
export interface ForceElementSizeOptions {
/**
* The element to target.
*/
target: DOMElementRequest;
/**
* The width to override.
*
* @defaultvalue 'auto'
*/
widthValue?: number | string;
/**
* The height to override.
*
* @defaultvalue 'auto'
*/
heightValue?: number | string;
/**
* Force body width to `widthValue`.
*
* @defaultvalue true
*/
forceWidth?: boolean;
/**
* Force body width to `heightValue`.
*
* @defaultvalue true
*/
forceHeight?: boolean;
/**
* When no element is found matching the target, should the script raise an
* error?
*
* @defaultValue false
*/
shouldThrowWhenNotFound?: boolean;
}

const defaultOptions: ForceElementSizeOptions = {
forceHeight: true,
forceWidth: true,
widthValue: 'auto',
heightValue: 'auto',
shouldThrowWhenNotFound: false
} as ForceElementSizeOptions;

/**
* This feature sets element size programmatically and only once, when
* {@link https://developer.mozilla.org/fr/docs/Web/Events/DOMContentLoaded | DOMContentLoaded}
* has been fired.
*
* @public
*/
export const ForceElementSizeFeature: FeatureConstructor<ForceElementSizeOptions> = new FeatureBuilder<
ForceElementSizeOptions
>({
script,
defaultOptions,
featureIdentifier: 'org.formidable-webview/webshell.force-element-size',
className: 'ForceElementSizeFeature'
}).build();
25 changes: 25 additions & 0 deletions packages/core/src/features/ForceElementSizeFeature.webjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function ForceElementSizeFeature(context) {
var options = context.options || {};
var widthValue = options.widthValue || 'auto';
var heightValue = options.heightValue || 'auto';
var forceWidth = options.forceWidth !== false;
var forceHeight = options.forceHeight !== false;
var target = options.target;
var shouldThrowWhenNotFound = options.shouldThrowWhenNotFound === true;
if (!target || (typeof target !== 'string' && typeof target !== 'object')) {
throw new Error('Missing or wrong type for required target option');
}
var element = context.getDOMSelection(target, false);
if (element) {
if (forceWidth) {
element.style.width = widthValue;
}
if (forceHeight) {
element.style.height = heightValue;
}
} else if (shouldThrowWhenNotFound) {
throw new Error(
"Couldn't find an element for target " + JSON.stringify(target)
);
}
}

0 comments on commit f715b5f

Please sign in to comment.