Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes loading element icon color #24551

Merged
merged 4 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions x-pack/plugins/canvas/common/lib/hex_to_rgb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const hexToRgb = hex => {
const shorthandHexColor = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i;
const hexColor = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice regexes and approach!

What are our requirements here, eg. do we need to support 4-element hexas with alpha of CSS4?

Copy link
Contributor Author

@cqliu1 cqliu1 Oct 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isColorDark service provided by EUI only checks RGB, so I'm not sure that we need the alpha value, but it'd be good to test. We also accept rgb, rgba, and color names as background colors (basically anything CSS will accept), so maybe I should support them here as well.

const shorthandMatches = shorthandHexColor.exec(hex);
if (shorthandMatches) return shorthandMatches.slice(1, 4).map(hex => parseInt(hex + hex, 16));

const hexMatches = hexColor.exec(hex);
if (hexMatches) return hexMatches.slice(1, 4).map(hex => parseInt(hex, 16));

return null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React from 'react';
import expect from 'expect.js';
import { shallow } from 'enzyme';
import { EuiLoadingSpinner, EuiIcon } from '@elastic/eui';
import { Loading } from '../';
import { Loading } from '../loading';

describe('<Loading />', () => {
it('uses EuiIcon by default', () => {
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/canvas/public/components/loading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { pure } from 'recompose';
import { connect } from 'react-redux';
import { getSelectedPage, getPageById } from '../../state/selectors/workpad';
import { Loading as Component } from './loading';

export const Loading = pure(Component);
const mapStateToProps = state => ({
backgroundColor: getPageById(state, getSelectedPage(state)).style.background,
});

export const Loading = connect(mapStateToProps)(Component);
10 changes: 7 additions & 3 deletions x-pack/plugins/canvas/public/components/loading/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import React from 'react';
import PropTypes from 'prop-types';
import { EuiLoadingSpinner, EuiIcon } from '@elastic/eui';
import { EuiLoadingSpinner, EuiIcon, isColorDark } from '@elastic/eui';
import { hexToRgb } from '../../../common/lib/hex_to_rgb';

export const Loading = ({ animated, text }) => {
export const Loading = ({ animated, text, backgroundColor }) => {
if (animated) {
return (
<div className="canvasLoading">
Expand All @@ -23,6 +24,8 @@ export const Loading = ({ animated, text }) => {
);
}

const rgb = hexToRgb(backgroundColor);

return (
<div className="canvasLoading">
{text && (
Expand All @@ -31,14 +34,15 @@ export const Loading = ({ animated, text }) => {
&nbsp;
</span>
)}
<EuiIcon type="clock" />
<EuiIcon color={rgb && isColorDark(...rgb) ? 'ghost' : 'text'} type="clock" />
</div>
);
};

Loading.propTypes = {
animated: PropTypes.bool,
text: PropTypes.string,
backgroundColor: PropTypes.string,
};

Loading.defaultProps = {
Expand Down