UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

78 lines (77 loc) 2.96 kB
"use client"; import React from 'react'; import classnames from 'classnames'; import Context from "../../shared/Context.js"; import { isTrue, extendPropsWithContext } from "../../shared/component-helper.js"; import Space from "../space/Space.js"; import { getColor } from "../../shared/helpers.js"; const defaultProps = { element: 'section' }; export default function Section(localProps) { return React.createElement(Space, SectionParams(localProps)); } export function SectionParams(localProps) { const context = React.useContext(Context); const props = extendPropsWithContext(localProps, defaultProps, context.Section); const { variant, breakout = !props.outset, outset, roundedCorner, textColor, backgroundColor, dropShadow, outline, outlineWidth = typeof props.outline === 'undefined' && typeof props.outlineWidth === 'undefined' ? 'none' : props.outlineWidth, innerRef, className, children, spacing, style_type, inner_ref, ...attributes } = props; const internalRef = React.useRef(); const elementRef = innerRef || inner_ref || internalRef; return Object.freeze({ ...attributes, className: classnames(`dnb-section dnb-section--${variant ? variant : style_type || 'default'}`, className, spacing && `dnb-section--spacing-${isTrue(spacing) ? 'large' : spacing}`), 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?.style }, innerRef: elementRef, 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) { if (typeof media?.[size] !== 'undefined') { const value = valueCallback(media?.[size]); if (typeof value === 'string') { result[`--${name}--${size}`] = value; } } } return result; } Section._name = 'Section'; Section._supportsSpacingProps = true; //# sourceMappingURL=Section.js.map