Skip to content

Commit

Permalink
Use CSS modules (#15)
Browse files Browse the repository at this point in the history
* fix(docs): add GalleryPaginationItem to table of contents [skip-ci]

* refactor: use CSS modules

* 4.0.0

* fix: react dependency version

* fix: root render

* fix: react dependency order

* fix: root dep

---------

Co-authored-by: Marlon Marcello <[email protected]>
  • Loading branch information
andrewrubin and marlonmarcello authored Feb 16, 2024
1 parent b74b9b6 commit 8d99621
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 55 deletions.
8 changes: 2 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
<title>@wethegit/react-gallery</title>
<style>
body {
overscroll-behavior: none;
}

.gallery {
overflow: hidden;
overflow-x: hidden;
}

img {
max-width: 100%;
inline-size: 100%;
}
</style>
</head>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/react-gallery",
"version": "3.0.4",
"version": "4.0.0",
"description": "A customizable, accessible gallery component for React projects.",
"files": [
"dist"
Expand Down
13 changes: 7 additions & 6 deletions src/lib/components/gallery-context.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// packages
import PropTypes from "prop-types"
import { createContext, useCallback, useRef, useState, useEffect } from "react"

// utils
import classnames from "../utils/classnames"

// styles
import "./gallery.css"
import styles from "./gallery.module.css"

export const GalleryContext = createContext()

Expand Down Expand Up @@ -118,13 +115,17 @@ export const Gallery = ({
return (
<GalleryContext.Provider value={value}>
<div
className={classnames(["gallery", draggable && "gallery--draggable", className])}
className={classnames([
styles.gallery,
draggable && styles["gallery--draggable"],
className,
])}
style={{ "--touch-offset": touchState.xOffset }}
>
{children}

{ariaLiveText && (
<p aria-live="polite" className="gallery-util-visually-hidden">
<p aria-live="polite" className={styles["visually-hidden"]}>
{ariaLiveText.replace("$i", activeIndex + 1).replace("$t", items.length)}
</p>
)}
Expand Down
22 changes: 10 additions & 12 deletions src/lib/components/gallery-item.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// packages
import PropTypes from "prop-types"

// hooks
import { useGallery } from "../hooks/use-gallery"

// utils
import classnames from "../utils/classnames"

import styles from "./gallery.module.css"

export const GalleryItem = ({ index, active, className, children, ...props }) => {
const {
itemNodes,
Expand All @@ -28,15 +26,15 @@ export const GalleryItem = ({ index, active, className, children, ...props }) =>
{...a11yProps}
{...props}
className={classnames([
"gallery__item",
draggable && "gallery__item--draggable",
draggable && touchState.offsetting && "gallery__item--dragging",
active && "gallery__item--active",
index === previouslyActiveIndex && "gallery__item--was-active",
index < activeIndex && "gallery__item--left",
index > activeIndex && "gallery__item--right",
styles.gallery__item,
draggable && styles["gallery__item--draggable"],
draggable && touchState.offsetting && styles["gallery__item--dragging"],
active && styles["gallery__item--active"],
index === previouslyActiveIndex && styles["gallery__item--was-active"],
index < activeIndex && styles["gallery__item--left"],
index > activeIndex && styles["gallery__item--right"],
(visibleRange === -1 || Math.abs(index - activeIndex) <= visibleRange) &&
"gallery__item--visible",
styles["gallery__item--visible"],
className,
])}
style={{
Expand Down
8 changes: 3 additions & 5 deletions src/lib/components/gallery-main.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// packages
import PropTypes from "prop-types"
import { useCallback } from "react"

// hooks
import { useGallery } from "../hooks/use-gallery"

// utils
import classnames from "../utils/classnames"

import styles from "./gallery.module.css"

export const GalleryMain = ({ renderGalleryItem, className, ...props }) => {
const {
activeIndex,
Expand Down Expand Up @@ -122,7 +120,7 @@ export const GalleryMain = ({ renderGalleryItem, className, ...props }) => {

return (
<ul
className={classnames(["gallery__main", className])}
className={classnames([styles.gallery__main, className])}
onPointerDown={draggable ? handlePointerDown : null}
onPointerMove={draggable ? handlePointerMove : null}
onPointerUp={draggable ? handlePointerUp : null}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/components/gallery-nav.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// packages
import PropTypes from "prop-types"

// hooks
import { useGallery } from "../hooks/use-gallery"

// utils
import classnames from "../utils/classnames"

import styles from "./gallery.module.css"

export const GalleryNav = ({
direction,
renderNavItem,
Expand Down Expand Up @@ -38,9 +36,9 @@ export const GalleryNav = ({
return (
<button
className={classnames([
styles.gallery__nav,
styles[`gallery__nav--${direction ? "next" : "previous"}`],
className,
"gallery__nav",
`gallery__nav--${direction ? "next" : "previous"}`,
])}
onClick={handleClick}
{...(!loop && disabledProps)}
Expand Down
17 changes: 8 additions & 9 deletions src/lib/components/gallery-pagination-item.jsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
// packages
import PropTypes from "prop-types"

// hooks
import { useGallery } from "../hooks/use-gallery"

// utils
import classnames from "../utils/classnames"

import styles from "./gallery.module.css"

export const GalleryPaginationItem = ({
index,
active,
className,
buttonClassName,
buttonProps,
children,
onClick,
...props
}) => {
const { goToIndex, itemNodes } = useGallery()

const handleClick = (i) => (event) => {
const handleClick = (i) => {
goToIndex(i)
itemNodes.current[i].focus({ preventScroll: true })
onClick?.({ event, index })
}

return (
<li className={classnames(["gallery__pagination-item", className])} {...props}>
<li
className={classnames([styles["gallery__pagination-item"], className])}
{...props}
>
<button
className={buttonClassName}
onClick={handleClick(index)}
aria-current={active ? "true" : null}
onClick={() => handleClick(index)}
{...buttonProps}
>
{children}
Expand Down
8 changes: 3 additions & 5 deletions src/lib/components/gallery-pagination.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// packages
import PropTypes from "prop-types"

// hooks
import { useGallery } from "../hooks/use-gallery"

// utils
import classnames from "../utils/classnames"

import styles from "./gallery.module.css"

/***
*
* Pagination Item component callback
Expand Down Expand Up @@ -37,7 +35,7 @@ export const GalleryPagination = ({ renderPaginationItem, className, ...props })
const { activeIndex, galleryItems } = useGallery()

return (
<ul className={classnames(["gallery__pagination", className])} {...props}>
<ul className={classnames([styles.gallery__pagination, className])} {...props}>
{galleryItems.map((item, index) => {
const active = activeIndex === index
return renderPaginationItem({ index, active, activeIndex, item })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.gallery-util-visually-hidden {
.visually-hidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
Expand Down Expand Up @@ -36,14 +36,12 @@
}

.gallery__item {
/* stylelint-disable */
--x: calc(
(var(--touch-offset, 0) * 1px) + var(--position-offset) +
(var(--active-gap) * var(--side)) + (var(--item-width) + var(--gap)) *
(var(--i) - var(--selected))
);
--delay: calc(0s * var(--center-offset, 0));
/* stylelint-enable */

grid-area: item;
margin: 0 auto;
Expand All @@ -56,6 +54,7 @@

.gallery__item--draggable img {
pointer-events: none;
user-select: none;
}

.gallery__item--dragging {
Expand Down
5 changes: 5 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export default defineConfig({
},
},
},
css: {
modules: {
generateScopedName: "[name]__[local]__[hash:base64:6]",
},
},
})

0 comments on commit 8d99621

Please sign in to comment.