|
|
|
|
|
|
|
|
import { cloneLayout, compact, correctBounds } from "./utils"; |
|
|
|
|
|
import type { CompactType, Layout } from "./utils"; |
|
|
|
|
|
export type Breakpoint = string; |
|
|
export type DefaultBreakpoints = "lg" | "md" | "sm" | "xs" | "xxs"; |
|
|
|
|
|
|
|
|
export type ResponsiveLayout<T: Breakpoint> = { |
|
|
+[breakpoint: T]: Layout |
|
|
}; |
|
|
export type Breakpoints<T: Breakpoint> = { |
|
|
+[breakpoint: T]: number |
|
|
}; |
|
|
|
|
|
export type OnLayoutChangeCallback = ( |
|
|
Layout, |
|
|
{ [key: Breakpoint]: Layout } |
|
|
) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getBreakpointFromWidth( |
|
|
breakpoints: Breakpoints<Breakpoint>, |
|
|
width: number |
|
|
): Breakpoint { |
|
|
const sorted = sortBreakpoints(breakpoints); |
|
|
let matching = sorted[0]; |
|
|
for (let i = 1, len = sorted.length; i < len; i++) { |
|
|
const breakpointName = sorted[i]; |
|
|
if (width > breakpoints[breakpointName]) matching = breakpointName; |
|
|
} |
|
|
return matching; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getColsFromBreakpoint( |
|
|
breakpoint: Breakpoint, |
|
|
cols: Breakpoints<Breakpoint> |
|
|
): number { |
|
|
if (!cols[breakpoint]) { |
|
|
throw new Error( |
|
|
"ResponsiveReactGridLayout: `cols` entry for breakpoint " + |
|
|
breakpoint + |
|
|
" is missing!" |
|
|
); |
|
|
} |
|
|
return cols[breakpoint]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findOrGenerateResponsiveLayout( |
|
|
layouts: ResponsiveLayout<Breakpoint>, |
|
|
breakpoints: Breakpoints<Breakpoint>, |
|
|
breakpoint: Breakpoint, |
|
|
lastBreakpoint: Breakpoint, |
|
|
cols: number, |
|
|
compactType: CompactType |
|
|
): Layout { |
|
|
|
|
|
if (layouts[breakpoint]) return cloneLayout(layouts[breakpoint]); |
|
|
|
|
|
let layout = layouts[lastBreakpoint]; |
|
|
const breakpointsSorted = sortBreakpoints(breakpoints); |
|
|
const breakpointsAbove = breakpointsSorted.slice( |
|
|
breakpointsSorted.indexOf(breakpoint) |
|
|
); |
|
|
for (let i = 0, len = breakpointsAbove.length; i < len; i++) { |
|
|
const b = breakpointsAbove[i]; |
|
|
if (layouts[b]) { |
|
|
layout = layouts[b]; |
|
|
break; |
|
|
} |
|
|
} |
|
|
layout = cloneLayout(layout || []); |
|
|
return compact(correctBounds(layout, { cols: cols }), compactType, cols); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function sortBreakpoints( |
|
|
breakpoints: Breakpoints<Breakpoint> |
|
|
): Array<Breakpoint> { |
|
|
const keys: Array<string> = Object.keys(breakpoints); |
|
|
return keys.sort(function (a, b) { |
|
|
return breakpoints[a] - breakpoints[b]; |
|
|
}); |
|
|
} |
|
|
|