All files / packages/design-system/shared/components ColorToken.jsx

71.42% Statements 10/14
37.5% Branches 3/8
50% Functions 1/2
71.42% Lines 10/14

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                                                                        1x           1x             1x 1x 1x 1x   1x       1x                                                             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 } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
 
export class Tokens extends Component {
  constructor(props) {
    super(props);
    this.state = {
      open: this.props.open
    };
  }
 
  render() {
    return (
      <div className="doc-tokens-group">
        <button
          className="slds-button"
          onClick={() => this.setState({ open: !this.state.open })}
        >
          {this.state.open ? 'Hide' : 'Show'} Tokens Used {this.props.text}
        </button>
        <div
          className={classNames(
            this.state.open ? 'slds-is-expanded' : 'slds-is-collapsed'
          )}
        >
          <div className="slds-grid slds-wrap">{this.props.children}</div>
        </div>
      </div>
    );
  }
}
 
Tokens.propTypes = {
  open: PropTypes.bool,
  text: PropTypes.string,
  children: PropTypes.node
};
 
Tokens.defaultProps = {
  open: false
};
 
class Token extends Component {
  render() {
    let tokenValue;
    let { token, tokenSet } = this.props;
    let tokenPath = require(`../../design-tokens/dist/${tokenSet}.json`);
    let set = JSON.stringify(tokenPath);
    JSON.parse(set, (key, value) => {
      if (key === token) {
        tokenValue = value;
      }
    });
 
    return (
      <div
        className={classNames(
          'doc-token slds-media slds-media_center',
          this.props.grid && 'slds-size_1-of-2 slds-large-size_1-of-3'
        )}
      >
        <div className="slds-media__figure">
          {tokenValue && (
            <div className="doc-token__image">
              <div style={{ backgroundColor: tokenValue }} />
            </div>
          )}
        </div>
        <div className="slds-media__body">
          <p>
            <strong>Name: </strong>
            <div title={token} className="slds-truncate">
              {token}
            </div>
          </p>
          <p>
            <strong>Value: </strong>
            <span title={tokenValue}>{tokenValue}</span>
          </p>
        </div>
      </div>
    );
  }
}
 
Token.propTypes = {
  token: PropTypes.string,
  tokenSet: PropTypes.string,
  grid: PropTypes.bool
};
 
Token.defaultProps = {
  tokenSet: 'ui-force'
};
 
export default Token;