All files / packages/design-system/ui/components/builder-header index.jsx

70.27% Statements 52/74
70.37% Branches 19/27
66.66% Functions 8/12
70.27% Lines 52/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587                                1x 4x                                     1x                                       1x 5x                               1x 4x                                                                 1x         1x               4x                     1x       1x               4x 4x 4x                                                   4x   4x           4x                                                                   1x             1x 4x                                             1x 4x                                               1x             1x 2x                                                         1x 2x                                                                         1x 1x 1x                                                   1x           1x 1x   1x                                                   1x                         2x 2x 2x   2x                   2x                 2x 2x 2x                                                                                             1x                       2x 2x                             1x                                           4x   4x                                                 1x                       1x          
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React, { Component, useState } from 'react';
import PropTypes from 'prop-types';
import uniqueId from 'lodash.uniqueid';
import SvgIcon from '../../shared/svg-icon';
import ButtonIcon from '../button-icons/';
import PopoverExperience from '../popovers';
import { UtilityIcon } from '../icons/base/example';
import { Tooltip } from '../tooltips/base/example';
import { Menu, MenuList, MenuItem } from '../menus/dropdown/example';
 
/**
 * App name and logo
 */
const Logo = () => (
  <div className="slds-builder-header__item">
    <div className="slds-builder-header__item-label slds-media slds-media_center">
      <div className="slds-media__figure">
        <UtilityIcon
          containerClassName="slds-current-color"
          className="slds-icon_x-small"
          symbol="builder"
          assistiveText={false}
          title={false}
        />
      </div>
      <div className="slds-media__body">App Name</div>
    </div>
  </div>
);
 
/**
 * Dropdown menu in header
 */
const BuilderMenu = () => (
  <Menu className="slds-dropdown_right slds-dropdown_actions">
    <MenuList ariaLabel="Show More">
      <li
        className="slds-dropdown__header slds-truncate"
        role="presentation"
        tabIndex="0"
      >
        Menu Header
      </li>
      <MenuItem>Menu Item One</MenuItem>
      <MenuItem>Menu Item Two</MenuItem>
      <MenuItem>Menu Item Three</MenuItem>
    </MenuList>
  </Menu>
);
 
/**
 * Navigation list in header
 */
const Nav = props => (
  <nav className="slds-builder-header__item slds-builder-header__nav">
    <ul className="slds-builder-header__nav-list">
      {props.showDropDown && (
        <NavItem
          symbol="page"
          showDropDown={props.showDropDown}
          openDropDown={props.openDropDown}
        />
      )}
    </ul>
  </nav>
);
 
/**
 * Navigation item in header
 */
const NavItem = ({ openDropDown, symbol }) => (
  <li className="slds-builder-header__nav-item slds-dropdown-trigger slds-dropdown-trigger_click slds-is-open">
    <button
      className="slds-button slds-builder-header__item-action slds-media slds-media_center"
      aria-haspopup="true"
      aria-expanded={openDropDown}
      title="Click to open menu"
    >
      <span className="slds-media__figure">
        <UtilityIcon
          containerClassName="slds-current-color"
          className="slds-icon_x-small"
          symbol={symbol}
          assistiveText={false}
          title={false}
        />
      </span>
      <span className="slds-media__body">
        <span className="slds-truncate" title="Dropdown">
          Dropdown
        </span>
        <UtilityIcon
          containerClassName="slds-current-color slds-m-left_small"
          className="slds-icon_x-small"
          symbol="chevrondown"
          assistiveText={false}
          title={false}
        />
      </span>
    </button>
    {openDropDown && <BuilderMenu />}
  </li>
);
 
NavItem.propTypes = {
  openDropDown: PropTypes.bool,
  symbol: PropTypes.string
};
 
NavItem.defaultProps = {
  openDropDown: false
};
 
/**
 * Document Name
 */
const Name = ({ label }) => {
  return (
    <div className="slds-builder-header__item slds-has-flexi-truncate">
      <h1 className="slds-builder-header__item-label">
        <span className="slds-truncate" title={label}>
          {label}
        </span>
      </h1>
    </div>
  );
};
 
Name.propTypes = {
  label: PropTypes.string
};
 
Name.defaultProps = {
  label: 'Page Type'
};
 
/**
 * Back Link
 */
const BackLink = ({ hasBackTooltip }) => {
  const [showTooltip, setTooltip] = useState(hasBackTooltip);
  const [hasFocus, setFocus] = useState(false);
  const [hasHover, setHover] = useState(false);
 
  const onFocus = () => {
    setFocus(true);
    setTooltip(true);
  };
 
  const onBlur = () => {
    setFocus(false);
    if (!hasHover) {
      setTooltip(null);
    }
  };
 
  const onMouseEnter = () => {
    setHover(true);
    setTooltip(true);
  };
 
  const onMouseLeave = () => {
    setHover(false);
    if (!hasFocus) {
      setTooltip(null);
    }
  };
 
  const exampleId = uniqueId('example-unique-id-');
 
  const computedTooltipStyles = {
    position: 'absolute',
    top: '100%',
    marginTop: '15px'
  };
 
  return (
    <div className="slds-builder-header__item">
      <a
        href="#"
        className="slds-builder-header__item-action"
        onClick={e => e.preventDefault()}
        onFocus={onFocus}
        onBlur={onBlur}
        onMouseEnter={onMouseEnter}
        onMouseLeave={onMouseLeave}
        aria-describedby={showTooltip && exampleId}
        title="Back"
      >
        <UtilityIcon
          containerClassName="slds-current-color"
          className="slds-icon_x-small"
          symbol="back"
          assistiveText="Back"
          title={false}
        />
      </a>
      {showTooltip && (
        <Tooltip
          nubbinPosition="top left"
          id={exampleId}
          style={computedTooltipStyles}
        >
          Back
        </Tooltip>
      )}
    </div>
  );
};
 
BackLink.propTypes = {
  hasBackTooltip: PropTypes.bool
};
 
/**
 * Help Link
 */
const Help = props => (
  <div className="slds-builder-header__utilities-item">
    <a
      href="#"
      className="slds-builder-header__item-action slds-media slds-media_center"
      onClick={e => e.preventDefault()}
    >
      <div className="slds-media__figure">
        <UtilityIcon
          containerClassName="slds-current-color"
          className="slds-icon_x-small"
          symbol="help"
          assistiveText={false}
          title={false}
        />
      </div>
      <div className="slds-media__body">Help</div>
    </a>
  </div>
);
 
/**
 * Settings
 */
const Settings = ({ symbol }) => (
  <div className="slds-builder-header__utilities-item">
    <a
      href="#"
      className="slds-builder-header__item-action slds-media slds-media_center"
      onClick={e => e.preventDefault()}
    >
      <span className="slds-media__figure">
        <UtilityIcon
          containerClassName="slds-current-color"
          className="slds-icon_x-small"
          symbol={symbol}
          assistiveText={false}
          title={false}
        />
      </span>
      <span className="slds-media__body">
        <span className="slds-truncate" title="Link">
          Settings
        </span>
      </span>
    </a>
  </div>
);
 
Settings.propTypes = {
  symbol: PropTypes.string
};
 
/**
 * Toolbar - Canvas Actions
 */
const CanvasActions = props => (
  <div className="slds-builder-toolbar__item-group" aria-label="Canvas Actions">
    <ButtonIcon
      className="slds-button_icon-border"
      symbol="settings"
      assistiveText="Canvas Settings"
      title="Canvas Settings"
    />
    <div className="slds-button-group">
      <ButtonIcon
        className="slds-button_icon-border"
        symbol="undo"
        assistiveText="Undo"
        title="Undo"
        tabIndex="0"
      />
      <ButtonIcon
        className="slds-button_icon-border"
        symbol="redo"
        assistiveText="Redo"
        title="Redo"
        tabIndex="-1"
      />
    </div>
  </div>
);
 
/**
 * Toolbar - Edit Actions
 */
const EditActions = props => (
  <div className="slds-builder-toolbar__item-group" aria-label="Edit actions">
    <div className="slds-button-group">
      <ButtonIcon
        className="slds-button_icon-border"
        symbol="cut"
        assistiveText="Cut"
        title="Cut"
        tabIndex="-1"
      />
      <ButtonIcon
        className="slds-button_icon-border"
        symbol="copy"
        assistiveText="Copy"
        title="Copy"
        tabIndex="-1"
      />
      <ButtonIcon
        className="slds-button_icon-border"
        symbol="paste"
        assistiveText="Paste"
        title="Paste"
        tabIndex="-1"
      />
    </div>
    <ButtonIcon
      className="slds-button_icon-border"
      symbol="toggle_panel_left"
      assistiveText="Toggle Panel"
      title="Toggle Panel"
    />
  </div>
);
 
/**
 * Toolbar - Status Text
 */
const Status = ({ hasStatusTooltip }) => {
  const [showTooltip, setTooltip] = useState(hasStatusTooltip);
  const [hasFocus, setFocus] = useState(false);
  const [hasHover, setHover] = useState(false);
 
  const onFocus = () => {
    setFocus(true);
    setTooltip(true);
  };
 
  const onBlur = () => {
    setFocus(false);
    if (!hasHover) {
      setTooltip(null);
    }
  };
 
  const onMouseEnter = () => {
    setHover(true);
    setTooltip(true);
  };
 
  const onMouseLeave = () => {
    setHover(false);
    if (!hasFocus) {
      setTooltip(null);
    }
  };
 
  const computedTooltipStyles = {
    position: 'absolute',
    top: '100%',
    marginTop: '15px'
  };
 
  const computedButtonClassnames = 'slds-button slds-p-horizontal_small';
  const exampleId = uniqueId('example-unique-id-');
 
  return (
    <>
      {showTooltip && (
        <Tooltip
          nubbinPosition="top left"
          id={exampleId}
          style={computedTooltipStyles}
        >
          Version saved by Johnny Appleseed Today 9:00 AM
        </Tooltip>
      )}
      <button
        className={computedButtonClassnames}
        aria-describedby={showTooltip && exampleId}
        aria-live="polite"
        onFocus={onFocus}
        onBlur={onBlur}
        onMouseEnter={onMouseEnter}
        onMouseLeave={onMouseLeave}
      >
        Inactive - Modified 1 day ago
      </button>
    </>
  );
};
 
Status.propTypes = {
  hasStatusTooltip: PropTypes.bool
};
 
/**
 * Toolbar - Document actions
 */
const DocumentActions = ({
  hasStatus,
  hasStatusTooltip,
  hasWarning,
  hasError
}) => {
  let alertType = [];
  hasWarning && alertType.push('warning');
  hasError && alertType.push('error');
 
  const [activeError, setActiveError] = useState(false);
 
  const onClick = () => {
    hasError && setActiveError(true);
  };
 
  const callbackFunction = childData => {
    setActiveError(childData);
  };
 
  return (
    <div
      className="slds-builder-toolbar__actions"
      aria-label="Document actions"
    >
      {hasStatus && <Status hasStatusTooltip={hasStatusTooltip} />}
      {Array.isArray(alertType) &&
        alertType.length > 0 &&
        alertType.map((type, idx) => {
          const isWarning = type === 'warning';
          const isError = type === 'error';
          return (
            <PopoverExperience
              size="medium"
              type={type}
              assistiveText={type}
              title={type}
              key={type + idx}
              isWarning={isWarning}
              isError={isError}
              activeError={activeError && isError}
              parentCallback={callbackFunction}
              parentCallbackData={false}
            >
              <ButtonIcon />
            </PopoverExperience>
          );
        })}
      <button
        className="slds-button slds-button_neutral"
        disabled={activeError}
        onClick={onClick}
      >
        <SvgIcon
          className="slds-button__icon slds-button__icon_left"
          sprite="utility"
          symbol="right"
        />{' '}
        Run
      </button>
      <button
        className="slds-button slds-button_neutral"
        disabled={activeError}
        onClick={onClick}
      >
        Save As
      </button>
      <button
        className="slds-button slds-button_brand"
        disabled={activeError}
        onClick={onClick}
      >
        Save
      </button>
    </div>
  );
};
 
DocumentActions.propTypes = {
  hasStatus: PropTypes.bool,
  hasStatusTooltip: PropTypes.bool,
  hasWarning: PropTypes.bool,
  hasError: PropTypes.bool
};
 
/**
 * Builder Toolbar
 */
class BuilderToolbar extends Component {
  render() {
    const { hasStatus, hasStatusTooltip, hasWarning, hasError } = this.props;
    return (
      <div className="slds-builder-toolbar" role="toolbar">
        <CanvasActions />
        <EditActions />
        <DocumentActions
          hasStatus={hasStatus}
          hasStatusTooltip={hasStatusTooltip}
          hasWarning={hasWarning}
          hasError={hasError}
        />
      </div>
    );
  }
}
 
BuilderToolbar.propTypes = {
  hasStatus: PropTypes.bool,
  hasStatusTooltip: PropTypes.bool,
  hasWarning: PropTypes.bool,
  hasError: PropTypes.bool
};
 
/**
 * Builder Header
 */
class BuilderHeader extends Component {
  render() {
    const {
      showToolbar,
      docName,
      showDropDown,
      openDropDown,
      hasBackTooltip,
      hasStatus,
      hasStatusTooltip,
      hasWarning,
      hasError
    } = this.props;
 
    return (
      <div className="slds-builder-header_container">
        <header className="slds-builder-header">
          <BackLink hasBackTooltip={hasBackTooltip} />
          <Logo />
          <Nav showDropDown={showDropDown} openDropDown={openDropDown} />
          <Name label={docName} />
          <div className="slds-builder-header__item slds-builder-header__utilities">
            <Settings symbol="settings" />
            <Help />
          </div>
        </header>
        {showToolbar && (
          <BuilderToolbar
            hasStatus={hasStatus}
            hasStatusTooltip={hasStatusTooltip}
            hasWarning={hasWarning}
            hasError={hasError}
          />
        )}
      </div>
    );
  }
}
 
BuilderHeader.propTypes = {
  showToolbar: PropTypes.bool,
  docName: PropTypes.string,
  showDropDown: PropTypes.bool,
  openDropDown: PropTypes.bool,
  hasBackTooltip: PropTypes.bool,
  hasStatus: PropTypes.bool,
  hasStatusTooltip: PropTypes.bool,
  hasWarning: PropTypes.bool,
  hasError: PropTypes.bool
};
 
BuilderHeader.defaultProps = {
  showDropDown: true
};
 
export default BuilderHeader;