@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
96 lines (95 loc) • 3.92 kB
JavaScript
"use client";
import React from 'react';
import clsx from 'clsx';
import Context from "../../shared/Context.js";
import { extendPropsWithContext } from "../../shared/component-helper.js";
import Space from "../space/Space.js";
import Theme from "../../shared/Theme.js";
import { getColor } from "../../shared/helpers.js";
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
const defaultProps = {
element: 'section'
};
function SectionInstance(localProps) {
return _jsx(Space, {
...SectionParams(localProps)
});
}
export default function Section(props) {
return _jsx(SectionInstance, {
...props
});
}
export function SectionParams(localProps) {
var _localProps$surface, _context$theme, _ref;
const context = React.useContext(Context);
const props = extendPropsWithContext(localProps, defaultProps, context.Section, {
surface: (_localProps$surface = localProps === null || localProps === void 0 ? void 0 : localProps.surface) !== null && _localProps$surface !== void 0 ? _localProps$surface : context === null || context === void 0 || (_context$theme = context.theme) === null || _context$theme === void 0 ? void 0 : _context$theme.surface
});
const {
variant,
breakout = !props.outset,
outset,
roundedCorner,
textColor,
backgroundColor,
dropShadow,
outline,
outlineWidth = typeof props.outline === 'undefined' && typeof props.outlineWidth === 'undefined' ? 'none' : props.outlineWidth,
surface,
ref: refProp,
className,
children,
...attributes
} = props;
const internalRef = React.useRef(undefined);
const elementRef = refProp || internalRef;
return Object.freeze({
...attributes,
className: clsx(`dnb-section dnb-section--${(_ref = variant !== null && variant !== void 0 ? variant : surface) !== null && _ref !== void 0 ? _ref : 'default'}`, className, surface && `dnb-section--surface-${surface}`),
style: {
...computeStyle(breakout, 'breakout', value => `var(--breakout--${value ? 'on' : 'off'})`),
...computeStyle(outset, 'outset', value => value ? '1' : '0'),
...computeStyle(roundedCorner, 'rounded-corner', value => typeof value === 'boolean' ? value && 'var(--rounded-corner--value)' : value.map(v => v ? 'var(--rounded-corner--value)' : '0').join(' ')),
...computeStyle(textColor, 'text-color', value => getColor(value)),
...computeStyle(backgroundColor, 'background-color', value => getColor(value)),
...computeStyle(dropShadow, 'drop-shadow', value => value && 'var(--shadow-default)'),
...computeStyle(outline, 'outline-color', value => typeof value === 'boolean' ? value && 'var(--outline-color--value)' : getColor(value)),
...computeStyle(outlineWidth, 'outline-width', value => typeof value === 'number' ? `${value}px` : value),
...(attributes === null || attributes === void 0 ? void 0 : attributes.style)
},
ref: elementRef,
children: surface ? _jsx(Theme.Context, {
surface: surface,
children: children
}) : children
});
}
function computeStyle(property, name, valueCallback) {
let media = property;
if (media !== null && (Array.isArray(media) || typeof media !== 'object')) {
media = {
small: property,
medium: property,
large: property
};
}
const result = {};
for (const size in media) {
var _media;
if (typeof ((_media = media) === null || _media === void 0 ? void 0 : _media[size]) !== 'undefined') {
var _media2;
const value = valueCallback((_media2 = media) === null || _media2 === void 0 ? void 0 : _media2[size]);
if (typeof value === 'string') {
result[`--${name}--${size}`] = value;
}
}
}
return result;
}
Section._name = 'Section';
withComponentMarkers(Section, {
_supportsSpacingProps: true
});
//# sourceMappingURL=Section.js.map