|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {inject, InjectionToken} from '@angular/core'; |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * A breakpoint is a wrapper interface around the browser's mediaQuery, |
| 13 | + * which is a condition string used for matching based on browser window |
| 14 | + * parameters. Here, a breakpoint has a shortcut name, e.g. 'xs', the |
| 15 | + * corresponding mediaQuery, and a priority in case the mediaQuery overlaps |
| 16 | + * with other registered breakpoints. |
| 17 | + * |
| 18 | + * @see https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries |
| 19 | + */ |
| 20 | +export interface Breakpoint { |
| 21 | + /** The shortcut name for the breakpoint, e.g. 'xs' */ |
| 22 | + name: string; |
| 23 | + /** The mediaQuery for the breakpoint, e.g. 'screen and (max-width: 500px)' */ |
| 24 | + media: string; |
| 25 | + /** The priority of the breakpoint compared to other breakpoints */ |
| 26 | + priority: number; |
| 27 | +} |
| 28 | + |
| 29 | +export const FALLBACK_BREAKPOINT_KEY: string = '__FALLBACK__'; |
| 30 | + |
| 31 | +/** |
| 32 | + * The fallback breakpoint, which has no real name and is |
| 33 | + * superseded by any other breakpoint value |
| 34 | + */ |
| 35 | +export const FALLBACK_BREAKPOINT: Breakpoint = { |
| 36 | + name: FALLBACK_BREAKPOINT_KEY, |
| 37 | + media: 'all', |
| 38 | + priority: -Number.MAX_SAFE_INTEGER, |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * The default breakpoints as provided by Google's Material Design. |
| 43 | + * These do not include orientation breakpoints or device breakpoints. |
| 44 | + */ |
| 45 | +export const DEFAULT_BREAKPOINTS: Breakpoint[] = [ |
| 46 | + { |
| 47 | + name: 'xs', |
| 48 | + media: 'screen and (min-width: 0px) and (max-width: 599.9px)', |
| 49 | + priority: 1000, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'sm', |
| 53 | + media: 'screen and (min-width: 600px) and (max-width: 959.9px)', |
| 54 | + priority: 900, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: 'md', |
| 58 | + media: 'screen and (min-width: 960px) and (max-width: 1279.9px)', |
| 59 | + priority: 800, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: 'lg', |
| 63 | + media: 'screen and (min-width: 1280px) and (max-width: 1919.9px)', |
| 64 | + priority: 700, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: 'xl', |
| 68 | + media: 'screen and (min-width: 1920px) and (max-width: 4999.9px)', |
| 69 | + priority: 600, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'lt-sm', |
| 73 | + media: 'screen and (max-width: 599.9px)', |
| 74 | + priority: 950, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: 'lt-md', |
| 78 | + media: 'screen and (max-width: 959.9px)', |
| 79 | + priority: 850, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: 'lt-lg', |
| 83 | + media: 'screen and (max-width: 1279.9px)', |
| 84 | + priority: 750, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'lt-xl', |
| 88 | + priority: 650, |
| 89 | + media: 'screen and (max-width: 1919.9px)', |
| 90 | + }, |
| 91 | + { |
| 92 | + name: 'gt-xs', |
| 93 | + media: 'screen and (min-width: 600px)', |
| 94 | + priority: -950, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'gt-sm', |
| 98 | + media: 'screen and (min-width: 960px)', |
| 99 | + priority: -850, |
| 100 | + }, { |
| 101 | + name: 'gt-md', |
| 102 | + media: 'screen and (min-width: 1280px)', |
| 103 | + priority: -750, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: 'gt-lg', |
| 107 | + media: 'screen and (min-width: 1920px)', |
| 108 | + priority: -650, |
| 109 | + } |
| 110 | +]; |
| 111 | + |
| 112 | +/** |
| 113 | + * The user-facing injection token for providing breakpoints, |
| 114 | + * this is meant to be provided as a multi-provider, and |
| 115 | + * consolidated later. |
| 116 | + */ |
| 117 | +export const BREAKPOINTS = |
| 118 | + new InjectionToken<Array<Array<Breakpoint>>>('Angular Layout Breakpoints'); |
| 119 | + |
| 120 | +/** An internal-facing provider for the default breakpoints */ |
| 121 | +export const BREAKPOINTS_PROVIDER = { |
| 122 | + provide: BREAKPOINTS, |
| 123 | + useValue: DEFAULT_BREAKPOINTS, |
| 124 | + multi: true, |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * An internal-facing injection token to consolidate all registered |
| 129 | + * breakpoints for use in the application. |
| 130 | + */ |
| 131 | +export const BPS = new InjectionToken<Breakpoint[]>('Angular Layout Condensed Breakpoints', { |
| 132 | + providedIn: 'root', |
| 133 | + factory: () => { |
| 134 | + const providedBps = inject(BREAKPOINTS); |
| 135 | + const bpMap: Map<string, Breakpoint> = new Map(); |
| 136 | + |
| 137 | + providedBps.forEach(bps => { |
| 138 | + bps.forEach(bp => { |
| 139 | + bpMap.set(bp.name, bp); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + return [...Array.from(bpMap.values()), FALLBACK_BREAKPOINT]; |
| 144 | + } |
| 145 | +}); |
0 commit comments