Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Refactor `SCREEN_SIZES` to match `z-indices.js` (#1985)

Dhruv Bhanushali0c1caa0a8784
diff --git a/src/components/VFooter/VFooter.vue b/src/components/VFooter/VFooter.vue
index b99606eb9..87d172265 100644
--- a/src/components/VFooter/VFooter.vue
+++ b/src/components/VFooter/VFooter.vue
@@ -113,7 +113,7 @@ export default defineComponent({
const footerEl = ref<HTMLElement | null>(null)
const { dimens: footerDimens } = useResizeObserver(footerEl)
const variantNames = computed(() =>
- Array.from(SCREEN_SIZES)
+ Object.entries(SCREEN_SIZES)
.filter(([, val]) => footerDimens.value.width >= val)
.map(([key]) => `footer-${key}`)
)
diff --git a/src/composables/use-media-query.ts b/src/composables/use-media-query.ts
index d50a9fa18..b4cf9df3d 100644
--- a/src/composables/use-media-query.ts
+++ b/src/composables/use-media-query.ts
@@ -89,7 +89,7 @@ export const isMinScreen = (
}
const query = computed(() => {
- const sizeInPx = SCREEN_SIZES.get(resolvedBp)
+ const sizeInPx = SCREEN_SIZES[resolvedBp]
return `(min-width: ${sizeInPx}px)`
})
diff --git a/src/constants/screens.js b/src/constants/screens.js
index 4757b1c14..7c4801b93 100644
--- a/src/constants/screens.js
+++ b/src/constants/screens.js
@@ -1,43 +1,62 @@
/**
- * This file cannot be converted to TypeScript because it's used by the Tailwind config
+ * This file cannot be converted to TypeScript because it's referenced in the
+ * Tailwind config. `RealBreakpoints` excludes 'xs' because it's not defined in
+ * Tailwind (being mobile-first) but is used in JavaScript to refer to mobile
+ * screens that are narrower than 640px ('sm').
+ *
+ * @typedef {'2xl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs'} Breakpoint
+ * @typedef {Exclude<Breakpoint, 'xs'>} RealBreakpoint
+ * @typedef {{ name: string, styles: { width: string, height: string }}} Viewport
*/
-/** @typedef {'2xl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs'} Breakpoint */
/**
- * Mapping of a breakpoint name to the lower-bound of its screen-width range
+ * mapping of breakpoint names to the lower-bound of their screen width range
*/
-const SCREEN_SIZES = new Map(
- /** @type {const} */ ([
- ['2xl', 1536],
- ['xl', 1280],
- ['lg', 1024],
- ['md', 768],
- ['sm', 640],
- ])
+const SCREEN_SIZES = Object.freeze(
+ /** @type {const} */ ({
+ '2xl': 1536,
+ xl: 1280,
+ lg: 1024,
+ md: 768,
+ sm: 640,
+ })
)
-const viewportEntries = [
- ...Array.from(SCREEN_SIZES),
- /** @type {const} */ (['xs', 340]),
-].map(([key, val]) => {
- return /** @type {const} */ ([
- key,
- {
- name: `${key} (${val}px)`,
- styles: { width: `${val}px`, height: '768px' },
- },
- ])
-})
+/**
+ * mapping of breakpoint names to the lower-bound of their screen width range,
+ * with the 'px' suffix attached; This is used by Tailwind.
+ */
+const SCREENS = /** @type {Record<RealBreakpoint, string>} */ (
+ Object.fromEntries(
+ Object.entries(SCREEN_SIZES)
+ .map(([key, val]) => [key, `${val}px`])
+ .reverse()
+ )
+)
-const VIEWPORTS =
- /** @type {Record<Breakpoint, typeof viewportEntries[number][1]>} */ (
- Object.fromEntries(viewportEntries)
+/**
+ * mapping of breakpoint names to viewport specifications; This is used by
+ * the viewports plugin of Storybook.
+ */
+const VIEWPORTS = /** @type {Record<Breakpoint, Viewport>} */ (
+ Object.fromEntries(
+ /** @type {[Breakpoint, number][]} */ (Object.entries(SCREEN_SIZES))
+ .concat([['xs', 340]])
+ .map(([key, val]) => [
+ key,
+ {
+ name: `${key} (${val}px)`,
+ styles: { width: `${val}px`, height: '768px' },
+ },
+ ])
)
+)
/**
- * This module is consumed by Nuxt and Tailwind config so it needs to use CJS modules
+ * This module is consumed by Nuxt and Tailwind config, so it needs to use CJS modules.
*/
module.exports = {
SCREEN_SIZES,
+ SCREENS,
VIEWPORTS,
}
diff --git a/tailwind.config.js b/tailwind.config.js
index f00502195..7f8badaa3 100644
--- a/tailwind.config.js
+++ b/tailwind.config.js
@@ -1,4 +1,4 @@
-const { SCREEN_SIZES } = require('./src/constants/screens')
+const { SCREENS } = require('./src/constants/screens')
const { Z_INDICES } = require('./src/constants/z-indices')
module.exports = {
@@ -8,12 +8,7 @@ module.exports = {
'./tailwind.safelist.txt',
],
theme: {
- screens: Object.fromEntries(
- Array.from(SCREEN_SIZES, ([name, width]) => [
- name,
- `${width}px`,
- ]).reverse()
- ),
+ screens: SCREENS,
zIndex: Z_INDICES,
colors: {
// Accents
diff --git a/test/playwright/utils/breakpoints.ts b/test/playwright/utils/breakpoints.ts
index e7835d9e8..2fa8cf57e 100644
--- a/test/playwright/utils/breakpoints.ts
+++ b/test/playwright/utils/breakpoints.ts
@@ -100,7 +100,7 @@ const makeBreakpointDescribe =
}
const capitalize = (s: string): Capitalize<typeof s> =>
- `${s[0].toUpperCase()}${s.slice(1)}`
+ `${s[0].toUpperCase()}${s.slice(1)}` as Capitalize<typeof s>
const breakpointTests = Array.from(Object.entries(VIEWPORTS)).reduce(
(
@@ -113,10 +113,11 @@ const breakpointTests = Array.from(Object.entries(VIEWPORTS)).reduce(
]
) =>
Object.assign(tests, {
- [`describe${capitalize(breakpoint)}`]: makeBreakpointDescribe(
- breakpoint as Breakpoint,
- parseFloat(width.replace('px', ''))
- ),
+ [`describe${capitalize(breakpoint as Breakpoint)}`]:
+ makeBreakpointDescribe(
+ breakpoint as Breakpoint,
+ parseFloat(width.replace('px', ''))
+ ),
}),
{} as Record<
`describe${Capitalize<Breakpoint>}`,
diff --git a/test/playwright/utils/navigation.ts b/test/playwright/utils/navigation.ts
index c4cf5582f..16d505eb4 100644
--- a/test/playwright/utils/navigation.ts
+++ b/test/playwright/utils/navigation.ts
@@ -73,8 +73,6 @@ export const renderModes = ['SSR', 'CSR'] as const
export type RenderMode = typeof renderModes[number]
export type LanguageDirection = 'ltr' | 'rtl'
-const smWidth = SCREEN_SIZES.get('sm') as number
-
export const buttonSelectors = {
filter: 'button[aria-controls="filters"]',
contentSwitcher: 'button[aria-controls="content-switcher-modal"]',
@@ -324,7 +322,7 @@ export const selectHomepageSearchType = async (
dir: LanguageDirection = 'ltr'
) => {
const pageWidth = page.viewportSize()?.width
- if (pageWidth && pageWidth > smWidth) {
+ if (pageWidth && pageWidth > SCREEN_SIZES.sm) {
await page.click(`[aria-label="${t('search-type.all', dir)}"]`)
await page.click(
`button[role="radio"]:has-text("${searchTypeNames[dir][searchType]}")`