Skip to content

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

Make z-index usage in JavaScript consistent with Tailwind (#1984)

Dhruv Bhanushali37a0eb31c8c2
diff --git a/src/components/VPopover/VPopover.vue b/src/components/VPopover/VPopover.vue
index 3e8a1a340..f4162b659 100644
--- a/src/components/VPopover/VPopover.vue
+++ b/src/components/VPopover/VPopover.vue
@@ -52,6 +52,8 @@ import {
PropType,
} from '@nuxtjs/composition-api'
+import { zIndexValidator } from '~/constants/z-indices'
+
import VPopoverContent from '~/components/VPopover/VPopoverContent.vue'
import type { Placement, PositioningStrategy } from '@popperjs/core'
@@ -129,10 +131,9 @@ export default defineComponent({
* the z-index to apply to the popover content
*/
zIndex: {
- type: Number,
- default: 50,
- // TODO: extract valid z-indexes (these are from the tailwind config)
- validator: (v: number) => [0, 10, 20, 30, 40, 50].includes(v),
+ type: [Number, String],
+ default: 'popover', // named z-index
+ validator: zIndexValidator,
},
/**
* Whether the popover height should be clipped and made scrollable
diff --git a/src/components/VPopover/VPopoverContent.vue b/src/components/VPopover/VPopoverContent.vue
index f22648069..d532e98a8 100644
--- a/src/components/VPopover/VPopoverContent.vue
+++ b/src/components/VPopover/VPopoverContent.vue
@@ -40,6 +40,8 @@ import { usePopoverContent } from '~/composables/use-popover-content'
import { warn } from '~/utils/console'
import { defineEvent } from '~/types/emits'
+import { zIndexValidator } from '~/constants/z-indices'
+
import type { CSSProperties } from '@vue/runtime-dom'
import type { SetupContext } from 'vue'
@@ -92,11 +94,9 @@ export default defineComponent({
validate: (v: string) => ['absolute', 'fixed'].includes(v),
},
zIndex: {
- type: Number,
+ type: [String, Number],
required: true,
- // TODO: extract valid z-indexes (these are from the tailwind config)
- validator: (v: number | 'auto') =>
- [0, 10, 20, 30, 40, 50, 'auto'].includes(v),
+ validator: zIndexValidator,
},
clippable: {
type: Boolean,
diff --git a/src/composables/use-popper.ts b/src/composables/use-popper.ts
index 1bdd683db..61260fa05 100644
--- a/src/composables/use-popper.ts
+++ b/src/composables/use-popper.ts
@@ -19,7 +19,7 @@ export type PopoverContentProps = {
triggerElement: HTMLElement | null
placement: Placement
strategy: PositioningStrategy
- zIndex: number
+ zIndex: number | string
}
type Props = {
diff --git a/src/constants/z-indices.js b/src/constants/z-indices.js
new file mode 100644
index 000000000..c13942083
--- /dev/null
+++ b/src/constants/z-indices.js
@@ -0,0 +1,38 @@
+/**
+ * This file cannot be converted to TypeScript because it's referenced in the
+ * Tailwind config.
+ */
+
+/**
+ * mapping of z-index names to z-indices; Ensure that these values are also
+ * written in the `tailwind.safelist.txt` file in the project root.
+ */
+const Z_INDICES = Object.freeze(
+ /** @type {const} */ ({
+ auto: 'auto',
+ 10: 10,
+ 20: 20,
+ 30: 30,
+ 40: 40,
+ 50: 50,
+ // Named indices
+ popover: 50,
+ })
+)
+
+/**
+ * Check whether the given z-index is valid and is configured in Tailwind.
+ *
+ * @param value {string|number} the provided z-index to validate
+ * @return {boolean} whether the z-index is valid and configured in Tailwind
+ */
+const zIndexValidator = (value) =>
+ Object.keys(Z_INDICES).includes(value.toString())
+
+/**
+ * This module is consumed by Nuxt and Tailwind config, so it needs to use CJS modules.
+ */
+module.exports = {
+ Z_INDICES,
+ zIndexValidator,
+}
diff --git a/tailwind.config.js b/tailwind.config.js
index a0b3fb119..f00502195 100644
--- a/tailwind.config.js
+++ b/tailwind.config.js
@@ -1,4 +1,5 @@
const { SCREEN_SIZES } = require('./src/constants/screens')
+const { Z_INDICES } = require('./src/constants/z-indices')
module.exports = {
content: [
@@ -13,6 +14,7 @@ module.exports = {
`${width}px`,
]).reverse()
),
+ zIndex: Z_INDICES,
colors: {
// Accents
tomato: '#e23600',
diff --git a/tailwind.safelist.txt b/tailwind.safelist.txt
index a3a4ec33a..df97666c8 100644
--- a/tailwind.safelist.txt
+++ b/tailwind.safelist.txt
@@ -1,5 +1,5 @@
-Dynamically generated classes are missed by Tailwind. Place files here to ensure
-that they are generated by Tailwind.
+Dynamically generated classes are missed by Tailwind. Place utilities here to
+ensure that they are generated by Tailwind.
w-4
w-5
@@ -12,3 +12,11 @@ h-5
h-6
h-8
h-12
+
+z-auto
+z-10
+z-20
+z-30
+z-40
+z-50
+z-popover