UNPKG

ndla-ui

Version:

UI component library for NDLA.

201 lines (176 loc) 6.93 kB
var _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; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component, Fragment } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { Forward } from 'ndla-icons/common'; import { Cross } from 'ndla-icons/action'; import SafeLink from '../common/SafeLink'; import SectionHeading from '../SectionHeading'; var classes = BEMHelper('c-subject-archive'); var SubjectArchive = function (_Component) { _inherits(SubjectArchive, _Component); function SubjectArchive(props) { _classCallCheck(this, SubjectArchive); var _this = _possibleConstructorReturn(this, (SubjectArchive.__proto__ || Object.getPrototypeOf(SubjectArchive)).call(this, props)); _this.state = { archiveOpen: false, minHeight: null }; _this.handleToggleArchive = _this.handleToggleArchive.bind(_this); _this.wrapperRef = null; return _this; } _createClass(SubjectArchive, [{ key: 'handleToggleArchive', value: function handleToggleArchive() { var _this2 = this; this.setState(function (prevState) { var newState = { archiveOpen: !prevState.archiveOpen }; if (!prevState.minHeight) { newState.minHeight = _this2.wrapperRef.offsetHeight; } return newState; }); } }, { key: 'render', value: function render() { var _this3 = this; var _props = this.props, fixedWidth = _props.fixedWidth, featuringArticle = _props.featuringArticle, messages = _props.messages, sectionHeading = _props.sectionHeading, archiveArticles = _props.archiveArticles; var archiveId = 'subject-archive'; var section = this.state.archiveOpen ? React.createElement( 'nav', _extends({ id: archiveId }, classes('archive')), React.createElement( 'ul', classes('archive-articles'), archiveArticles.map(function (article) { return React.createElement( 'li', { key: article.heading }, React.createElement( SafeLink, { to: article.url }, article.heading ) ); }) ) ) : React.createElement( 'section', classes('featuring'), React.createElement( 'div', classes('media-wrapper'), featuringArticle.media ), React.createElement( 'div', classes('content'), React.createElement( 'h1', classes('heading'), React.createElement( SafeLink, { to: featuringArticle.url }, featuringArticle.heading ) ), React.createElement( 'p', classes('description'), featuringArticle.description ) ) ); var wrapperStyles = this.state.minHeight ? { minHeight: this.state.minHeight } : null; return React.createElement( 'section', _extends({ style: wrapperStyles }, classes('', { fixedWidth: fixedWidth, animate: this.state.minHeight }), { ref: function ref(_ref) { _this3.wrapperRef = _ref; } }), React.createElement( SectionHeading, { large: true, className: classes('section-heading').className }, sectionHeading ), React.createElement( 'div', classes('wrapper'), section, React.createElement( 'button', { type: 'button', 'aria-expanded': this.state.archiveOpen, 'aria-controls': archiveId, className: classes('archive-button').className, onClick: this.handleToggleArchive }, this.state.archiveOpen ? React.createElement( Fragment, null, React.createElement(Cross, null), ' ', React.createElement( 'span', null, messages.close ) ) : React.createElement( Fragment, null, React.createElement(Forward, null), ' ', React.createElement( 'span', null, messages.archive ) ) ) ) ); } }]); return SubjectArchive; }(Component); SubjectArchive.propTypes = { featuringArticle: PropTypes.shape({ media: PropTypes.node.isRequired, heading: PropTypes.string.isRequired, description: PropTypes.string.isRequired, url: PropTypes.string.isRequired }).isRequired, archiveArticles: PropTypes.arrayOf(PropTypes.shape({ url: PropTypes.string.isRequired, heading: PropTypes.string.isRequired })).isRequired, sectionHeading: PropTypes.string.isRequired, fixedWidth: PropTypes.bool, messages: PropTypes.shape({ archive: PropTypes.string.isRequired }) }; SubjectArchive.defaultProps = { fixedWidth: false }; export default SubjectArchive;