Skip to content

Commit

Permalink
feat(core): Add breakpoints to theme
Browse files Browse the repository at this point in the history
  • Loading branch information
anicholls committed Oct 24, 2019
1 parent eaa4c5c commit d7b081f
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/banner/react/lib/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const BannerWrapper = styled('button')<BannerProps>(
},
({error, theme}) => {
theme = getTheme(theme); // eslint-disable-line no-param-reassign
console.log(theme.breakpoints.only('m'));
return {
backgroundColor:
error === ErrorType.Error ? theme.palette.error.main : theme.palette.alert.main,
Expand Down
4 changes: 4 additions & 0 deletions modules/core/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import spacing, {
} from './lib/spacing';
import type, {CanvasType, fontFamily, monoFontFamily, CanvasTypeVariant} from './lib/type';
import {CSSProperties} from './lib/types';
import {breakpoints, CanvasBreakpoints, BreakpointKey} from './lib/theming/breakpoints';

const {default: colors, ...semanticColors} = canvasColorsWeb;
const canvas = {
Expand All @@ -31,6 +32,7 @@ export * from './lib/TypeWrappers';
export * from '@workday/canvas-colors-web';
export {
borderRadius,
breakpoints,
colors,
depth,
spacing,
Expand All @@ -39,6 +41,8 @@ export {
fontFamily,
monoFontFamily,
BrandingColor,
BreakpointKey,
CanvasBreakpoints,
CanvasDepth,
CanvasDepthValue,
CanvasSpacing,
Expand Down
65 changes: 65 additions & 0 deletions modules/core/react/lib/theming/breakpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export enum BreakpointKey {
zero = 'zero',
s = 's',
m = 'm',
l = 'l',
xl = 'xl',
}

export type BreakpointFnParam = BreakpointKey | keyof typeof BreakpointKey;

export type CanvasBreakpoints = {
zero: number;
s: number;
m: number;
l: number;
xl: number;
[key: string]: number;
};

export const breakpointKeys = ['zero', 's', 'm', 'l', 'xl'];

export const breakpoints: CanvasBreakpoints = {
zero: 0,
s: 600,
m: 960,
l: 1280,
xl: 1920,
};

const step = 0.5;

export function up(key: BreakpointFnParam) {
const value = typeof breakpoints[key as BreakpointKey] === 'number' ? breakpoints[key] : key;
return `@media (min-width:${value}px)`;
}

export function down(key: BreakpointFnParam) {
const endIndex = breakpointKeys.indexOf(key as BreakpointKey) + 1;
const upperbound = breakpoints[breakpointKeys[endIndex]];

if (endIndex === breakpointKeys.length) {
// xl down applies to all sizes
return up(BreakpointKey.zero);
}

const value = typeof upperbound === 'number' && endIndex > 0 ? upperbound : 0;
return `@media (max-width:${value - step}px)`;
}

export function between(start: BreakpointFnParam, end: BreakpointFnParam) {
const endIndex = breakpointKeys.indexOf(end) + 1;

if (endIndex === breakpointKeys.length) {
return up(start);
}

return (
`@media (min-width:${breakpoints[start]}px) and ` +
`(max-width:${breakpoints[breakpointKeys[endIndex]] - step}px)`
);
}

export function only(key: BreakpointFnParam) {
return between(key, key);
}
9 changes: 8 additions & 1 deletion modules/core/react/lib/theming/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import colors from '@workday/canvas-colors-web';
import deepmerge from 'deepmerge';
import {CanvasTheme} from './types';
import {breakpoints, up, down, between, only} from './breakpoints';

/**
* Considerations:
Expand Down Expand Up @@ -41,8 +42,14 @@ export const defaultCanvasTheme: CanvasTheme = {
focusOutline: colors.blueberry400,
},
},
breakpoints: {
values: breakpoints,
up,
down,
between,
only,
},
// Typography
// Breakpoints
// Depth?
};

Expand Down
9 changes: 9 additions & 0 deletions modules/core/react/lib/theming/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {CanvasBreakpoints, BreakpointFnParam} from './breakpoints';

/**
* A single palette within a Canvas theme
*/
Expand Down Expand Up @@ -26,6 +28,13 @@ export interface CanvasTheme {
alert: CanvasThemePalette;
[index: string]: CanvasThemePalette | CanvasThemeCommonPalette;
};
breakpoints: {
values: CanvasBreakpoints;
up: (key: BreakpointFnParam) => string;
down: (key: BreakpointFnParam) => string;
only: (key: BreakpointFnParam) => string;
between: (start: BreakpointFnParam, end: BreakpointFnParam) => string;
};
}

/**
Expand Down
51 changes: 51 additions & 0 deletions modules/core/react/spec/breakpoints.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {BreakpointKey, up, down, between, only} from '../lib/theming/breakpoints';

describe('Breakpoints', () => {
test('up function works with enum', () => {
const mediaQuery = up(BreakpointKey.m);

expect(mediaQuery).toBe('@media (min-width:960px)');
});

test('up function works with string', () => {
const mediaQuery = up('m');

expect(mediaQuery).toBe('@media (min-width:960px)');
});

test('down function works with enum', () => {
const mediaQuery = down(BreakpointKey.m);

expect(mediaQuery).toBe('@media (max-width:1279.5px)');
});

test('down function works with string', () => {
const mediaQuery = down('m');

expect(mediaQuery).toBe('@media (max-width:1279.5px)');
});

test('between function works with enums', () => {
const mediaQuery = between(BreakpointKey.m, BreakpointKey.l);

expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1919.5px)');
});

test('between function works with string', () => {
const mediaQuery = between('m', 'l');

expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1919.5px)');
});

test('only function works with enums', () => {
const mediaQuery = only(BreakpointKey.m);

expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1279.5px)');
});

test('between function works with string', () => {
const mediaQuery = only('m');

expect(mediaQuery).toBe('@media (min-width:960px) and (max-width:1279.5px)');
});
});

0 comments on commit d7b081f

Please sign in to comment.