Skip to content

Commit

Permalink
Improve type safety by always returning screenLayout value
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-lb committed Jun 26, 2020
1 parent 35e2535 commit c0690bc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ Name | Type | Required | Default value | Description
{
width?: number;
height?: number;
screenLayout?: {
layout: xs | sm | md | lg | xl;
screenLayout: {
layout: xs | sm | md | lg | xl | undefined;

isXs: boolean;
isSm: boolean;
Expand Down
28 changes: 28 additions & 0 deletions src/constants/defaultScreenLayout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ScreenLayout } from '../interfaces';

/**
* Default screen layout.
*/
const defaultScreenLayout: ScreenLayout = {
layout: undefined,

isXs: false,
isSm: false,
isMd: false,
isLg: false,
isXl: false,

isXsOrBelow: false,
isSmOrBelow: false,
isMdOrBelow: false,
isLgOrBelow: false,
isXlOrBelow: false,

isXsOrAbove: false,
isSmOrAbove: false,
isMdOrAbove: false,
isLgOrAbove: false,
isXlOrAbove: false,
};

export default defaultScreenLayout;
4 changes: 2 additions & 2 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface GetCurrentLayoutOptions {
* Result for getCurrentLayout.
*/
export interface ScreenLayout {
layout: LayoutEnum;
layout?: LayoutEnum;

isXs: boolean;
isSm: boolean;
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface GetWindowSizeOptions {
export interface GetWindowSizeResult {
width?: number;
height?: number;
screenLayout?: ScreenLayout;
screenLayout: ScreenLayout;
}

/**
Expand Down
36 changes: 6 additions & 30 deletions src/utils/getCurrentLayout.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import { GetCurrentLayoutOptions, ScreenLayout } from '../interfaces';
import defaultScreenLayout from '../constants/defaultScreenLayout';
import LayoutEnum from '../constants/layoutEnum';

/**
* Default return options.
*/
const defaultReturn: ScreenLayout = {
layout: LayoutEnum.xs,

isXs: false,
isSm: false,
isMd: false,
isLg: false,
isXl: false,

isXsOrBelow: false,
isSmOrBelow: false,
isMdOrBelow: false,
isLgOrBelow: false,
isXlOrBelow: false,

isXsOrAbove: false,
isSmOrAbove: false,
isMdOrAbove: false,
isLgOrAbove: false,
isXlOrAbove: false,
};

/**
* Returns the current layout.
* @param param0 options
Expand All @@ -36,7 +12,7 @@ const getCurrentLayout = ({
}: GetCurrentLayoutOptions): ScreenLayout => {
if (width <= breakpoints.sm) {
return {
...defaultReturn,
...defaultScreenLayout,

// Current layout
layout: LayoutEnum.xs,
Expand All @@ -58,7 +34,7 @@ const getCurrentLayout = ({

if (width < breakpoints.md) {
return {
...defaultReturn,
...defaultScreenLayout,

// Current layout
layout: LayoutEnum.sm,
Expand All @@ -80,7 +56,7 @@ const getCurrentLayout = ({

if (width < breakpoints.lg) {
return {
...defaultReturn,
...defaultScreenLayout,

// Current layout
layout: LayoutEnum.md,
Expand All @@ -102,7 +78,7 @@ const getCurrentLayout = ({

if (width < breakpoints.xl) {
return {
...defaultReturn,
...defaultScreenLayout,

// Current layout
layout: LayoutEnum.lg,
Expand All @@ -123,7 +99,7 @@ const getCurrentLayout = ({
}

return {
...defaultReturn,
...defaultScreenLayout,

// Current layout
layout: LayoutEnum.xl,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/getWindowSize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetWindowSizeResult, GetWindowSizeOptions } from '../interfaces';
import defaultScreenLayout from '../constants/defaultScreenLayout';
import getCurrentLayout from './getCurrentLayout';

/**
Expand All @@ -14,7 +15,7 @@ const getWindowSize = ({
return {
width: undefined,
height: undefined,
screenLayout: undefined,
screenLayout: defaultScreenLayout,
};
}

Expand Down

0 comments on commit c0690bc

Please sign in to comment.