@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
89 lines • 3.16 kB
JavaScript
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
import React from 'react';
import classnames from 'classnames';
import { isTrue } from "../../../shared/component-helper.js";
import Section from "../../section/Section.js";
import ModalContext from "../ModalContext.js";
import CloseButton from "./CloseButton.js";
export default class ModalHeaderBar extends React.PureComponent {
constructor(props) {
super(props);
_defineProperty(this, "state", {
showShadow: false
});
this._ref = React.createRef();
}
componentDidMount() {
this.observeHeader();
}
componentDidUpdate(prevProps) {
if (prevProps.children !== this.props.children) {
var _this$intersectionObs;
(_this$intersectionObs = this.intersectionObserver) === null || _this$intersectionObs === void 0 || _this$intersectionObs.disconnect();
this.observeHeader();
}
}
componentWillUnmount() {
var _this$intersectionObs2;
(_this$intersectionObs2 = this.intersectionObserver) === null || _this$intersectionObs2 === void 0 || _this$intersectionObs2.disconnect();
}
observeHeader() {
if (typeof window !== 'undefined' && typeof IntersectionObserver !== 'undefined' && this._ref.current) {
const marginTop = -this._ref.current.clientHeight;
let scrollRoot = null;
let element = this._ref.current.parentElement;
while (element) {
var _element$classList;
if ((_element$classList = element.classList) !== null && _element$classList !== void 0 && _element$classList.contains('dnb-scroll-view')) {
scrollRoot = element;
break;
}
element = element.parentElement;
}
this.intersectionObserver = new IntersectionObserver(entries => {
const [entry] = entries;
this.setState({
showShadow: !entry.isIntersecting
});
}, {
root: scrollRoot,
rootMargin: `${marginTop}px 0px 0px 0px`,
threshold: 0.001
});
this.intersectionObserver.observe(this._ref.current);
}
}
render() {
const {
className = null,
children = null,
ref,
shadowClass = null,
...props
} = this.props;
const {
showShadow
} = this.state;
const {
hide_close_button = false,
close_button_attributes,
onCloseClickHandler,
close_title
} = this.context;
return React.createElement(Section, _extends({
style_type: "white",
className: classnames('dnb-modal__header__bar', className, showShadow && shadowClass),
inner_ref: this._ref
}, props), React.createElement("div", {
className: "dnb-modal__header__bar__inner"
}, children), !isTrue(hide_close_button) && React.createElement("div", {
className: "dnb-modal__header__bar__close"
}, React.createElement(CloseButton, _extends({
on_click: onCloseClickHandler,
close_title: close_title
}, close_button_attributes))));
}
}
_defineProperty(ModalHeaderBar, "contextType", ModalContext);
//# sourceMappingURL=ModalHeaderBar.js.map