Skip to content

Commit

Permalink
Added basic responsive utility components and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cchaos committed Jun 6, 2018
1 parent f70ae17 commit 649f517
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src-docs/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ import { PortalExample }
import { ProgressExample }
from './views/progress/progress_example';

import { ResponsiveExample }
from './views/responsive/responsive_example';

import { SearchBarExample }
from './views/search_bar/search_bar_example';

Expand Down Expand Up @@ -341,6 +344,7 @@ const navigation = [{
name: 'Utilities',
items: [
AccessibilityExample,
ResponsiveExample,
DelayHideExample,
ErrorBoundaryExample,
HighlightExample,
Expand Down
46 changes: 46 additions & 0 deletions src-docs/src/views/responsive/responsive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';

import {
EuiCode,
EuiHideFrom,
EuiShowFor,
} from '../../../../src/components';

export default () => (
<div>
<EuiHideFrom sizes={['xs']}>
Hiding from <EuiCode>xs</EuiCode> screens only
</EuiHideFrom>
<br/>
<EuiHideFrom sizes={['xs','s']}>
Hiding from <EuiCode>xs, s</EuiCode> screens
</EuiHideFrom>
<br/>
<EuiHideFrom sizes={['xs','s','m']}>
Hiding from <EuiCode>xs, s, m</EuiCode> screens
</EuiHideFrom>
<br/>
<EuiHideFrom sizes={['l']}>
Hiding from <EuiCode>l</EuiCode> screens only
</EuiHideFrom>

<br/>
<br/>

<EuiShowFor sizes={['xs']}>
Showing for <EuiCode>xs</EuiCode> screens only
</EuiShowFor>
<br/>
<EuiShowFor sizes={['xs','s']}>
Showing for <EuiCode>xs, s</EuiCode> screens
</EuiShowFor>
<br/>
<EuiShowFor sizes={['xs','s','m']}>
Showing for <EuiCode>xs, s, m</EuiCode> screens
</EuiShowFor>
<br/>
<EuiShowFor sizes={['l']}>
Showing for <EuiCode>l</EuiCode> screen only
</EuiShowFor>
</div>
);
40 changes: 40 additions & 0 deletions src-docs/src/views/responsive/responsive_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';

import { renderToHtml } from '../../services';

import {
GuideSectionTypes,
} from '../../components';

import {
EuiCode,
EuiShowFor,
EuiHideFrom,
} from '../../../../src/components';

import Responsive from './responsive';
const responsiveSource = require('!!raw-loader!./responsive');
const responsiveHtml = renderToHtml(Responsive);

export const ResponsiveExample = {
title: 'Responsive',
sections: [{
title: 'EuiShowFor and EuiHideFrom',
source: [{
type: GuideSectionTypes.JS,
code: responsiveSource,
}, {
type: GuideSectionTypes.HTML,
code: responsiveHtml,
}],
text: (
<p>
Pass an array of screen widths <EuiCode>[xs, s, m, l]</EuiCode> to either
the <EuiCode>EuiShowFor</EuiCode> or <EuiCode>EuiHideFrom</EuiCode> components
to make them responsive.
</p>
),
props: { EuiShowFor, EuiHideFrom },
demo: <Responsive />,
}],
};
21 changes: 21 additions & 0 deletions src-docs/src/views/utility_classes/utility_classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,26 @@ export default () => (

<EuiCode className="eui-displayInlineBlock">.eui-displayInlineBlock</EuiCode>

<h4>Responsive</h4>

<EuiCode className="euiHideFrom--xsmall">.euiHideFrom--xsmall</EuiCode>
<EuiSpacer />
<EuiCode className="euiHideFrom--small">.euiHideFrom--small</EuiCode>
<EuiSpacer />
<EuiCode className="euiHideFrom--medium">.euiHideFrom--medium</EuiCode>
<EuiSpacer />
<EuiCode className="euiHideFrom--large">.euiHideFrom--large</EuiCode>

<EuiSpacer />

<EuiCode className="euiShowFor--xsmall">.euiShowFor--xsmall</EuiCode>
<EuiSpacer />
<EuiCode className="euiShowFor--small">.euiShowFor--small</EuiCode>
<EuiSpacer />
<EuiCode className="euiShowFor--medium">.euiShowFor--medium</EuiCode>
<EuiSpacer />
<EuiCode className="euiShowFor--large">.euiShowFor--large</EuiCode>


</EuiText>
);
6 changes: 6 additions & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,9 @@ export {
EuiIconTip,
EuiToolTip,
} from './tool_tip';

export {
EuiHideFrom,
EuiShowFor,
} from './responsive';

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiHideFrom is rendered 1`] = `
<span
aria-label="aria-label"
class="euiHideFrom euiHideFrom--xsmall euiHideFrom--small euiHideFrom--medium euiHideFrom--large testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
9 changes: 9 additions & 0 deletions src/components/responsive/__snapshots__/show_for.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiShowFor is rendered 1`] = `
<span
aria-label="aria-label"
class="euiShowFor euiShowFor--xsmall euiShowFor--small euiShowFor--medium euiShowFor--large testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
48 changes: 48 additions & 0 deletions src/components/responsive/hide_from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

const responsiveSizesToClassNameMap = {
xs: 'euiHideFrom--xsmall',
s: 'euiHideFrom--small',
m: 'euiHideFrom--medium',
l: 'euiHideFrom--large',
}

const RESPONSIVE_SIZES = Object.keys(responsiveSizesToClassNameMap);

export const EuiHideFrom = ({
children,
className,
sizes,
...rest,
}) => {

const sizingClasses = sizes.map(function(item){
return responsiveSizesToClassNameMap[item];
});

const classes = classNames(
'euiHideFrom',
sizingClasses,
className
);

return (
<span
className={classes}
{...rest}
>
{children}
</span>
);
};

EuiHideFrom.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
/**
* List of all the responsive sizes to hide the children from
*/
sizes: PropTypes.arrayOf(PropTypes.oneOf(RESPONSIVE_SIZES)).isRequired,
};
16 changes: 16 additions & 0 deletions src/components/responsive/hide_from.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../test';

import { EuiHideFrom } from './hide_from';

describe('EuiHideFrom', () => {
test('is rendered', () => {
const component = render(
<EuiHideFrom sizes={['xs', 's', 'm', 'l']} {...requiredProps} />
);

expect(component)
.toMatchSnapshot();
});
});
7 changes: 7 additions & 0 deletions src/components/responsive/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
EuiHideFrom,
} from './hide_from';

export {
EuiShowFor,
} from './show_for';
48 changes: 48 additions & 0 deletions src/components/responsive/show_for.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

const responsiveSizesToClassNameMap = {
xs: 'euiShowFor--xsmall',
s: 'euiShowFor--small',
m: 'euiShowFor--medium',
l: 'euiShowFor--large',
}

const RESPONSIVE_SIZES = Object.keys(responsiveSizesToClassNameMap);

export const EuiShowFor = ({
children,
className,
sizes,
...rest,
}) => {

const sizingClasses = sizes.map(function(item){
return responsiveSizesToClassNameMap[item];
});

const classes = classNames(
'euiShowFor',
sizingClasses,
className
);

return (
<span
className={classes}
{...rest}
>
{children}
</span>
);
};

EuiShowFor.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
/**
* List of all the responsive sizes to show the children for
*/
sizes: PropTypes.arrayOf(PropTypes.oneOf(RESPONSIVE_SIZES)).isRequired,
};
16 changes: 16 additions & 0 deletions src/components/responsive/show_for.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../test';

import { EuiShowFor } from './show_for';

describe('EuiShowFor', () => {
test('is rendered', () => {
const component = render(
<EuiShowFor sizes={['xs', 's', 'm', 'l']} {...requiredProps} />
);

expect(component)
.toMatchSnapshot();
});
});
36 changes: 36 additions & 0 deletions src/global_styling/utility/_utility.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,39 @@
white-space: nowrap !important;
word-wrap: normal !important; /* 2 */
}

/**
* Responsive
*
* 3. Be sure to hide/show the element initially
*/
[class*="euiHideFrom"] {
display: initial !important; /* 3 */
}
[class*="euiShowFor"] {
display: none !important; /* 3 */
}
.euiHideFrom--xsmall {
@include screenXSmall() { display: none !important; }
}
.euiHideFrom--small {
@include screenSmall() { display: none !important; }
}
.euiHideFrom--medium {
@include screenMedium() { display: none !important; }
}
.euiHideFrom--large {
@include screenLarge() { display: none !important; }
}
.euiShowFor--xsmall {
@include screenXSmall() { display: initial !important; }
}
.euiShowFor--small {
@include screenSmall() { display: initial !important; }
}
.euiShowFor--medium {
@include screenMedium() { display: initial !important; }
}
.euiShowFor--large {
@include screenLarge() { display: initial !important; }
}

0 comments on commit 649f517

Please sign in to comment.