Nita's Digital Garden
💻Blog🚀GitHub💬micro.blog
  • 👋Welcome
    • Satoshi Nitawaki
      • Hey, I’m Satoshi
      • Uses
  • Digital Garden
    • GitBook
    • micro.blog
  • Photography
    • GR IIIx
  • Knowledge
    • PKM
      • 🆚AnyType vs Capacities
      • Capacities
      • Mindmap Tools
      • Note App Histories
      • Heptabase
    • Mental Models
      • Tech
        • Decision-Making Framework
        • Adopting an API-First / API Mandate Strategy
        • Don't using OOP way on Frontend DDD
        • KISS(Keep it Simple, Stupid)
        • Twelve-Factor App
      • Life
  • Tech
    • My Oshi
    • Frontend
      • Safe Data Fetching
      • SWR
      • Review Point
      • Assets
      • CSS Modules composition
    • Infrastructure
      • Online DDL / MySQL
    • TypeScript / JavaScript
      • 🦮Style Guides
      • Barrel Files
      • npm
    • CLI
      • Scaffolding Tool
  • Workflow
    • Dev Workflow
    • Knowledge Workflow
    • Book Workflow
  • Thought
    • Ideology
      • Getting Real
      • Design ideology
    • 結晶性知能
    • 流動性知能
    • ペット保険プリズムペット SBIプリズム少額短期保険
    • カーボローディング
    • エアコン掃除
    • ウォーターローディング
    • Game
      • How To Create Chart Per Deck Theme
      • Tournament Tool
    • Spotify Podcast
  • Awesome List
    • Dev Tools
    • Tools
    • AI Tools
  • CheatSheet
    • Git
    • Postmortem
    • ADR: Architecture Decision Records
    • Next
      • Next で tailwind スタイルの共存
  • AI
    • Deep Research Best Practice
Powered by GitBook
On this page
  1. CheatSheet
  2. Next

Next で tailwind スタイルの共存

#next #tailwind

import Image from 'next/image'
import { FC, HTMLAttributes } from 'react'

type TailwindRounded =
  | 'rounded-none'
  | 'rounded-sm'
  | 'rounded'
  | 'rounded-md'
  | 'rounded-lg'
  | 'rounded-xl'
  | 'rounded-2xl'
  | 'rounded-3xl'
  | 'rounded-full'

interface ImageWrapperProps {
  src: string
  alt: string
  width?: string
  height?: string
  rounded?: boolean | TailwindRounded
  className?: HTMLAttributes<HTMLDivElement>['className']
  priority?: boolean
  quality?: number
  sizes?: string
  objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down'
}

/**
 * @description Next Image Wrapper: for tailwindcss styling
 * @example
 * <ImageWrapper src="/images/logo.png" alt="logo" /> // 基本的な使用方法
 * @example
 * <ImageWrapper
 *   src="/path/to/image.jpg"
 *   alt="description"
 *   width="w-10"
 *   height="h-10"
 *   rounded={false}
 *   className="border border-gray-200"
 * /> // カスタマイズ
 * @param src Image URL
 * @param alt Image Alt
 * @param width Image Width
 * @param height Image Height
 * @param rounded Image Rounded
 * @param className Image Container Class
 * @param priority Image Priority
 * @param quality Image Quality: default 75
 * @param sizes Image Sizes
 * @param objectFit Image Object Fit: contain | cover | fill | none | scale-down
 * @ref https://nextjs.org/docs/pages/api-reference/components/image
 */
export const NextImage: FC<ImageWrapperProps> = ({
  src,
  alt,
  width = 'w-6',
  height = 'h-6',
  rounded = false,
  className = '',
  priority = false,
  quality = 75,
  sizes = '100%',
  objectFit = 'cover',
}) => {
  const roundedClass =
    typeof rounded === 'boolean' ? (rounded ? 'rounded' : '') : rounded
  return (
    <div className={`relative ${width} ${height} ${className}`}>
      <Image
        src={src}
        alt={alt}
        fill
        sizes={sizes}
        priority={priority}
        quality={quality}
        className={`object-${objectFit} ${roundedClass}`}
      />
    </div>
  )
}
PreviousNextNextAI

Last updated 3 months ago