1+ import type { OverridableStringUnion } from '../types' ;
2+
13export interface BreakpointOverrides { }
24
35export type Breakpoint = OverridableStringUnion <
@@ -96,7 +98,7 @@ export const breakpointKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
9698const sortBreakpointsValues = ( values : Breakpoints [ 'values' ] ) => {
9799 const breakpointsAsArray = Object . keys ( values ) . map ( key => ( {
98100 key,
99- val : values [ key ] ,
101+ val : values [ key as Breakpoint ] ,
100102 } ) ) || [ ] ;
101103
102104 // Sort in ascending order
@@ -109,7 +111,7 @@ const sortBreakpointsValues = (values: Breakpoints['values']) => {
109111 ...acc ,
110112 [ obj . key ] : obj . val ,
111113 } ;
112- } , { } ) ;
114+ } , { } as Breakpoints [ 'values' ] ) ;
113115} ;
114116
115117// Keep in mind that @media is inclusive by the CSS specification.
@@ -133,29 +135,42 @@ export default function createBreakpoints(breakpoints: BreakpointsOptions): Brea
133135 ...other
134136 } = breakpoints ;
135137 const sortedValues = sortBreakpointsValues ( values ) ;
136- const keys = Object . keys ( sortedValues ) ;
138+ const keys = Object . keys ( sortedValues ) as Breakpoint [ ] ;
137139
138140 function up ( key : Breakpoint | number ) {
139- const value = typeof values [ key ] === 'number' ? values [ key ] : key ;
141+ const value = typeof values [ key as Breakpoint ] === 'number'
142+ ? values [ key as Breakpoint ]
143+ : key ;
140144 return `@media (min-width:${ value } ${ unit } )` ;
141145 }
142146
143147 function down ( key : Breakpoint | number ) {
144- const value = typeof values [ key ] === 'number' ? values [ key ] : key ;
148+ const value = typeof values [ key as Breakpoint ] === 'number'
149+ ? values [ key as Breakpoint ]
150+ : key as number ;
145151 return `@media (max-width:${ value - step / 100 } ${ unit } )` ;
146152 }
147153
148154 function between ( start : Breakpoint | number , end : Breakpoint | number ) {
149- const endIndex = keys . indexOf ( end ) ;
155+ const endIndex = keys . indexOf ( end as Breakpoint ) ;
156+
150157 return (
151- `@media (min-width:${ typeof values [ start ] === 'number' ? values [ start ] : start } ${ unit } ) and `
152- + `(max-width:${ ( endIndex !== - 1 && typeof values [ keys [ endIndex ] ] === 'number' ? values [ keys [ endIndex ] ] : end ) - step / 100 } ${ unit } )`
158+ `@media (min-width:${
159+ typeof values [ start as Breakpoint ] === 'number'
160+ ? values [ start as Breakpoint ]
161+ : start
162+ } ${ unit } ) and `
163+ + `(max-width:${ (
164+ endIndex !== - 1 && typeof values [ keys [ endIndex ] as Breakpoint ] === 'number'
165+ ? values [ keys [ endIndex ] as Breakpoint ]
166+ : end as number
167+ ) - step / 100 } ${ unit } )`
153168 ) ;
154169 }
155170
156171 function only ( key : Breakpoint ) {
157172 if ( keys . indexOf ( key ) + 1 < keys . length ) {
158- return between ( key , keys [ keys . indexOf ( key ) + 1 ] ) ;
173+ return between ( key , keys [ keys . indexOf ( key ) + 1 ] as Breakpoint ) ;
159174 }
160175 return up ( key ) ;
161176 }
@@ -164,12 +179,12 @@ export default function createBreakpoints(breakpoints: BreakpointsOptions): Brea
164179 // handle first and last key separately, for better readability
165180 const keyIndex = keys . indexOf ( key ) ;
166181 if ( keyIndex === 0 ) {
167- return up ( keys [ 1 ] ) ;
182+ return up ( keys [ 1 ] as Breakpoint ) ;
168183 }
169184 if ( keyIndex === keys . length - 1 ) {
170- return down ( keys [ keyIndex ] ) ;
185+ return down ( keys [ keyIndex ] as Breakpoint ) ;
171186 }
172- return between ( key , keys [ keys . indexOf ( key ) + 1 ] ) . replace (
187+ return between ( key , keys [ keys . indexOf ( key ) + 1 ] as Breakpoint ) . replace (
173188 '@media' ,
174189 '@media not all and' ,
175190 ) ;
0 commit comments