@atlaskit/editor-plugin-expand
Version:
Expand plugin for @atlaskit/editor-core
60 lines (58 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.renderExpandButton = renderExpandButton;
var _styles = require("@atlaskit/editor-common/styles");
var _ui = require("@atlaskit/editor-common/ui");
/**
* Renders or updates the expand/collapse button inside the provided container element.
*
* @param container - The HTML element that will contain the expand/collapse button.
* @param buttonProps - Properties for configuring the button's behavior and appearance.
*/
function renderExpandButton(container, buttonProps) {
var allowInteractiveExpand = buttonProps.allowInteractiveExpand,
expanded = buttonProps.expanded,
intl = buttonProps.intl;
// Update existing button attributes
var label = intl.formatMessage(expanded ? _ui.expandMessages.collapseNode : _ui.expandMessages.expandNode);
var existingButton = container.querySelector(".".concat(_styles.expandClassNames.iconButton));
if (existingButton) {
existingButton.setAttribute('aria-label', label);
existingButton.setAttribute('aria-expanded', String(expanded));
if (allowInteractiveExpand) {
existingButton.setAttribute('title', label);
existingButton.removeAttribute('disabled');
} else {
existingButton.removeAttribute('title');
existingButton.setAttribute('disabled', 'true');
}
} else {
var doc = container.ownerDocument;
if (!doc) {
return;
}
var button = doc.createElement('button');
button.className = _styles.expandClassNames.iconButton;
button.setAttribute('aria-label', label);
button.setAttribute('aria-expanded', String(expanded));
if (allowInteractiveExpand) {
button.setAttribute('title', label);
} else {
button.setAttribute('disabled', 'true');
}
container.appendChild(button);
var svgIcon = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
svgIcon.setAttribute('class', _styles.expandClassNames.iconSvg);
svgIcon.setAttribute('width', '12');
svgIcon.setAttribute('height', '12');
svgIcon.setAttribute('viewBox', '0 0 16 16');
svgIcon.setAttribute('role', 'presentation');
var path = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('fill', 'currentcolor');
path.setAttribute('d', 'm6.03 1.47 6 6a.75.75 0 0 1 .052 1.004l-.052.056-6 6-1.06-1.06L10.44 8 4.97 2.53z');
svgIcon.appendChild(path);
button.appendChild(svgIcon);
}
}