Skip to content

Commit

Permalink
handle drupal header search action, style injection
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsonj committed Apr 15, 2021
1 parent 49bd5d8 commit 34beb41
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 12 deletions.
200 changes: 195 additions & 5 deletions lib/components/NeonHeader/NeonHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _htmlReactParser = _interopRequireDefault(require("html-react-parser"));
var _htmlReactParser = _interopRequireWildcard(require("html-react-parser"));

var _styles = require("@material-ui/core/styles");

var _useMediaQuery = _interopRequireDefault(require("@material-ui/core/useMediaQuery"));

var _Skeleton = _interopRequireDefault(require("@material-ui/lab/Skeleton"));

var _camelCase = _interopRequireDefault(require("lodash/camelCase"));

var _remoteAssetsMap = _interopRequireDefault(require("../../remoteAssetsMap/remoteAssetsMap"));

var _drupalHeader = _interopRequireDefault(require("../../remoteAssets/drupal-header.html"));
Expand All @@ -33,6 +35,8 @@ var _NeonContext = _interopRequireWildcard(require("../NeonContext/NeonContext")

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
Expand All @@ -54,7 +58,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
var DRUPAL_HEADER_HTML = _remoteAssetsMap.default.DRUPAL_HEADER_HTML.KEY;
var AUTH_ELEMENT_ID = 'header__authentication-ui';
var useStyles = (0, _styles.makeStyles)(function (theme) {
var _coreAuthContainer;
var _coreAuthContainer, _headerContainer;

return {
skeletonHeader: {
Expand Down Expand Up @@ -103,9 +107,184 @@ var useStyles = (0, _styles.makeStyles)(function (theme) {
display: 'block'
}
})
}
},
// Injecting these styles as a means of fixing up the search display
// Ideally, this CSS comes from Drupal and is removed from here...
headerContainer: (_headerContainer = {
'& .header__search': {
background: '#f5f6f7',
position: 'relative',
zIndex: 1,
transition: 'all 0.2s ease-in-out',
opacity: 1,
visibility: 'visible'
},
'& .header__search.visually-hidden': {
visibility: 'hidden',
opacity: 0,
transition: 'all 0.2s ease-in-out'
},
'& .header__search > .header__search--inner': {
display: 'flex',
maxWidth: '1620px',
margin: '0 auto'
},
'& .header__inner.l--offset-wide': {
paddingLeft: 'calc(2/27*100%)',
paddingRight: 'calc(2/27*100%)'
},
'& .header__search--inner': {
display: 'flex',
'-ms-flex-pack': 'start',
justifyContent: 'flex-start',
'-ms-flex-align': 'center',
alignItems: 'center'
},
'& .header__search--inner > .header__search--title': {
fontWeight: '600 !important',
fontSize: '0.9rem !important',
margin: '0 2.6rem 0 0 !important'
}
}, _defineProperty(_headerContainer, theme.breakpoints.up('lg'), {
'& .header__search--inner > .header__search--title': {
fontSize: '1rem !important'
}
}), _defineProperty(_headerContainer, '& .header__search--inner > .form-item', {
width: '100%',
display: 'flex',
'-ms-flex-pack': 'start',
justifyContent: 'flex-start',
'-ms-flex-align': 'center',
alignItems: 'center'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form', {
display: 'flex',
'-ms-flex-pack': 'start',
justifyContent: 'flex-start',
'-ms-flex-align': 'center',
alignItems: 'center',
width: '100%'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form > .form-item', {
width: '100%'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form > .form-item > .search-form__input', {
width: '100%',
background: '#fff',
border: '1px solid #d7d9d9',
boxSizing: 'border-box',
borderRadius: '3px 0 0 3px',
padding: '14px'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form > .search-form__actions > .search-form__button', {
borderRadius: '0px 3px 3px 0px',
width: '60px',
background: '#0073cf',
border: '#0073cf 1px solid',
color: 'transparent !important',
backgroundImage: 'url("data:image/svg+xml,%3Csvg%20width%3D%2220%22%20height%3D%2220%22%20viewBox%3D%220%200%2020%2020%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20fill-rule%3D%22evenodd%22%20clip-rule%3D%22evenodd%22%20d%3D%22M8%202a6%206%200%20100%2012A6%206%200%20008%202zM0%208a8%208%200%201114.32%204.906l5.387%205.387-1.414%201.414-5.387-5.387A8%208%200%20010%208z%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E")',
backgroundPosition: 'center center',
backgroundRepeat: 'no-repeat',
padding: '0.86rem 0',
transition: 'all 0.2s ease-in-out'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form > .search-form__actions > .search-form__button:hover', {
transition: 'all 0.2s ease-in-out',
backgroundColor: '#002c77',
border: '#002c77 1px solid'
}), _defineProperty(_headerContainer, '& .header__search--inner > form.search-form > label', {
fontWeight: 600,
fontSize: '20px',
margin: 'auto 2.6rem'
}), _defineProperty(_headerContainer, '& .header__search--inner > .header__search-close', {
margin: 'auto 2.5rem'
}), _defineProperty(_headerContainer, '& .header__search--inner > .header__search-close > button', {
background: 'none',
border: 'none',
width: '50px'
}), _defineProperty(_headerContainer, '& .header__search--inner > .header__search-close > button > svg', {
verticalAlign: 'middle'
}), _defineProperty(_headerContainer, '& .header__search--inner > .header__search-close > button > svg > path', {
transition: 'all 0.2s ease-in-out'
}), _defineProperty(_headerContainer, '& .header__search--inner > .header__search-close > button:hover > svg > path', {
fill: '#002c77',
transition: 'all 0.2s ease-in-out'
}), _defineProperty(_headerContainer, theme.breakpoints.down('md'), {
'& nav#block-neon-main-menu > ul > li.siteSearch': {
padding: '0 1.5rem 0.5rem 1.5rem'
},
'& .search-form-mobile': {
width: '100%'
},
'& form.search-form': {
display: 'flex',
'-ms-flex-pack': 'start',
justifyContent: 'flex-start',
'-ms-flex-align': 'center',
alignItems: 'center',
width: '100%'
},
'& form.search-form > .form-item': {
width: '100%'
},
'& form.search-form > .form-item > .search-form__input': {
width: '100%',
background: '#fff',
border: '1px solid #d7d9d9',
boxSizing: 'border-box',
borderRadius: '3px 0 0 3px',
padding: '0.88rem'
},
'& form.search-form > .search-form__actions > .search-form__button': {
borderRadius: '0px 3px 3px 0px',
width: '60px',
background: '#0073cf',
border: '#0073cf 1px solid',
color: 'transparent !important',
backgroundImage: 'url("data:image/svg+xml,%3Csvg%20width%3D%2220%22%20height%3D%2220%22%20viewBox%3D%220%200%2020%2020%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20fill-rule%3D%22evenodd%22%20clip-rule%3D%22evenodd%22%20d%3D%22M8%202a6%206%200%20100%2012A6%206%200%20008%202zM0%208a8%208%200%201114.32%204.906l5.387%205.387-1.414%201.414-5.387-5.387A8%208%200%20010%208z%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E")',
backgroundPosition: 'center center',
backgroundRepeat: 'no-repeat',
padding: '14px 0',
transition: 'all 0.2s ease-in-out'
},
'& form.search-form > .search-form__actions > .search-form__button:hover': {
transition: 'all 0.2s ease-in-out',
backgroundColor: '#002c77',
border: '#002c77 1px solid'
},
'& form.search-form > .form-item > label': {
display: 'none !important'
}
}), _headerContainer)
};
});

var buildSearchAction = function buildSearchAction(action) {
var root = 'https://www.neonscience.org';
if (!action) return "".concat(root, "/search/site");

if (action.startsWith('/')) {
return "".concat(root).concat(action);
}

return "".concat(root, "/").concat(action);
};

var applyAttribute = function applyAttribute(nextAttribs, attribs, attr) {
switch (attr) {
case 'class':
// eslint-disable-next-line no-param-reassign
nextAttribs.className = attribs[attr];
break;

default:
if (attr.includes('-')) {
// eslint-disable-next-line no-param-reassign
nextAttribs[(0, _camelCase.default)(attr)] = attribs[attr];
} else {
// eslint-disable-next-line no-param-reassign
nextAttribs[attr] = attribs[attr];
}

break;
}
};

var NeonHeader = /*#__PURE__*/(0, _react.forwardRef)(function (props, headerRef) {
var drupalCssLoaded = props.drupalCssLoaded,
unstickyDrupalHeader = props.unstickyDrupalHeader;
Expand Down Expand Up @@ -206,7 +385,18 @@ var NeonHeader = /*#__PURE__*/(0, _react.forwardRef)(function (props, headerRef)
var injectAuth = !auth.useCore ? null : {
replace: function replace(domNode) {
var _domNode$attribs = domNode.attribs,
attribs = _domNode$attribs === void 0 ? {} : _domNode$attribs;
attribs = _domNode$attribs === void 0 ? {} : _domNode$attribs,
name = domNode.name;

if (name === 'form' && attribs.class === 'search-form') {
var nextAttribs = {};
Object.keys(attribs).forEach(function (attr) {
applyAttribute(nextAttribs, attribs, attr);
});
return /*#__PURE__*/_react.default.createElement("form", _extends({}, nextAttribs, {
action: buildSearchAction(attribs.action)
}), (0, _htmlReactParser.domToReact)(domNode.children, injectAuth));
}

if (attribs.id !== AUTH_ELEMENT_ID) {
return null;
Expand All @@ -229,7 +419,7 @@ var NeonHeader = /*#__PURE__*/(0, _react.forwardRef)(function (props, headerRef)
return /*#__PURE__*/_react.default.createElement("header", {
ref: headerRef,
id: "header",
className: unstickyDrupalHeader ? classes.unstickyHeader : null
className: unstickyDrupalHeader ? "".concat(classes.unstickyHeader, " ").concat(classes.headerContainer) : classes.headerContainer
}, (0, _htmlReactParser.default)(html, injectAuth));
});
NeonHeader.propTypes = {
Expand Down
Loading

0 comments on commit 34beb41

Please sign in to comment.