"Utilize Tailwind to effortlessly place a single item at the center

Looking for a way to align a row of items with one item centered in the div and the others positioned on either side while maintaining their original order?

Check out this code snippet:

<div className="flex mx-auto justify-center items-center">
  <Circle selected={selected === ActionOptions.NONE} onClick={e => setSelected(ActionOptions.NONE)} title="None" />
  <Circle selected={selected === ActionOptions.ICON} onClick={e => setSelected(ActionOptions.ICON)} title="Icon">
    {Icon(Icons.CHECK, selected === ActionOptions.ICON ? 'w-20 h-20' : 'w-8 h-8')}
  </Circle>
  <Circle selected={selected === ActionOptions.EMOJI} onClick={e => setSelected(ActionOptions.EMOJI)} title="Emoji">
    {Icon(Icons.SMILE, selected === ActionOptions.EMOJI ? 'w-20 h-20' : 'w-8 h-8')}
  </Circle>
  <Circle selected={selected === ActionOptions.TIMER} onClick={e => setSelected(ActionOptions.TIMER)} title="Timer">
    <ClockIcon className={selected === ActionOptions.TIMER ? 'w-20 h-20' : 'w-8 h-8'} />
  </Circle>
</div>

function Circle({
  selected,
  children,
  onClick,
  title,
}: PropsWithChildren<{ selected: boolean; onClick: (e) => void; title: string }>) {
  return (
    <div className="flex flex-col items-center justify-center cursor-pointer">
      <div
        onClick={onClick}
        className={`rounded-full flex justify-center items-center ${
          selected ? 'w-32 h-32 border-4' : 'w-14 h-14 border-2'
        } mx-2  border-white bg-white/10`}
      >
        {children}
      </div>
      <span className="text-xs">{title}</span>
    </div>
  );
}

Does anyone have suggestions on how to achieve this layout design?

Answer №1

It seems like there may be some confusion about your request, but one possible solution could involve adjusting the flex-order in the following way:

const Shapes = [
  { name: 'Square', size: BoxOptions.SMALL, alignment: 1 },
  { name: 'Circle', size: BoxOptions.MEDIUM, alignment: 1 },
  { name: 'Triangle', size: BoxOptions.LARGE, alignment: 3 },
  { name: 'Hexagon', size: BoxOptions.XLARGE, alignment: 3 },
]

export default function Container() {
  const [selectedShape, setSelectedShape] = useState<BoxOptions>();

  return (
    <div className="flex mx-auto justify-center items-center transition-all duration-1000">
      {Shapes.map(({ name, size, alignment }) => {
        return (
          <Shape
            key={name}
            selected={selectedShape === size}
            onClick={() => setSelectedShape(size)}
            name={name}
            alignment={alignment}
          >
            <div>{size}</div>
          </Shape>
        );
      })}
    </div>
  );
}

export function Shape({ selected, children, onClick, name, alignment }) {
  return (
    <div
      className={`item flex flex-col items-center justify-center cursor-pointer ${
        selected ? `order-2` : `order-${alignment}`
      }`}
    >
      <div
        onClick={onClick}
        className={`rounded-full flex justify-center items-center ${
          selected ? 'w-32 h-32 border-4' : 'w-14 h-14 border-2'
        } mx-2  border-black bg-white/10`}
      >
        {children}
      </div>
      <span className="text-xs">{name}</span>
    </div>
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a table in React using an object with nested objects

I have 2 different types of JSON APIs and I want to showcase them in a table. The first type has the following structure: data1:[ { "id": 1, "name": "Leanne Graham", " ...

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...

What is the best way to remove a CSS style using JavaScript?

For my website automation using Selenium, I encountered a challenging dropdown that was not the standard native one but a custom-designed version. To tackle this issue, I needed to set its CSS class to hidden in order to access the native functionality smo ...

Tips for customizing the scrollbar design in iOS Safari

Task at hand requires me to customize the scrollbar style on my HTML page. Below is the CSS code I am using: .txt_Content::-webkit-scrollbar { width:5px; border-radius:4px; } .txt_Content::-webkit-scrollbar-button { display: none; } .txt_Co ...

Facing a react-native-camera issue during android compilation

After attempting to update my react native project to the latest version (0.59.2), I encountered an issue when trying to execute react-native run-android with this error: Could not establish the dependencies of task ':app:preDebugBuild'. > Co ...

I am unable to view the product even though its ID is visible in the URL

import Container from "@/app/components/Container"; import ProductDetails from "./ProductDetails"; import Reviews from "./Reviews"; import { Products } from "@/utilities/Products"; interface Iparams { productid?: ...

What is the best way to format text in React without using spaces?

Here's my take on the problem: https://codesandbox.io/s/fast-smoke-bhudq. The text does not wrap as expected, instead overflowing to the right beyond "HELLO". It appears that setting a maxWidth is not working effectively. Could someone please modify ...

Is there a way to make a single background image the frame for each individual post on a tumblr theme?

I've set my sights on a more challenging goal for my custom Tumblr themes - I want to have a background image for each post, similar to how other themes display posts within boxes. Is there a way to make these boxes into images? I've tried tinke ...

Adding Google Ads to a page with changing content using Next.js and React

After fetching articles from an API, I proceed to parse the HTML code: function Article({ article }) { return ( <PageContainer> <div className={styles.container}> {parser(article.content.html)} ...

What advantage does React gain by utilizing the previous state to update state instead of the current state?

I'm seeking to grasp why it's better to utilize the previous state rather than the current state to update an element in React. Despite seeing this approach commonly used in code examples and tutorials, I haven't come across a satisfactory e ...

Creating an interactive /robots.txt file for a Next.js application

I'm looking for a solution to dynamically respond to the /robots.txt request. To achieve this, I have chosen to utilize getServerSideProps https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering By exporting a ...

Update the next-auth session object once the user has been successfully updated through a patch request

I've scoured the depths of the Internet in search of an answer, but alas, my efforts have been fruitless. I attempted some tricks I found online, but they didn't do the trick for me. So, once a user logs in and a session is created, how can I up ...

Slide content horizontally to align with the opening and closing of the Drawer component in Material-UI

Here's a preview of my screen layout: https://i.stack.imgur.com/xGe3P.png The AppBar consistently appears above both the Drawer and Content, accomplished by utilizing Drawer variant="persistent". However, the text content remains stationary without ...

A missing semicolon is encountered while compiling a React application

I am currently working on my app using Create React App and incorporating scss. I have used a vscode plugin to convert the scss to css, but I keep encountering an error when running npm run build. The error message reads as follows: [email protected ...

Overflow is causing interference with the scrollY value

I've been attempting to retrieve the scrollY value in my React app, but it seems to be affected by overflow-related issues. Below is the code snippet I used to access the scrollY value: import React from "react"; import { useEffect, use ...

Verify that the text entered in the form is accurate, and if it meets the required criteria, proceed to the next

Is it possible to achieve this without using JavaScript? If not, I'd like to find the simplest solution. I have a form that functions similar to a password entry field, and I would like to redirect users to a certain page if they type in a specific p ...

The next.js script malfunctions once the router has been altered

My external scripts are not working after the router changes (**When the user clicks on any link**) within _app.js import Script from "next/script"; <Script async type="text/javascript" src= ...

The ReactJS Material UI inputbase component is not correctly extracting the input value

Need help extracting user input text from Material UI InputBase component I know I'm missing something, but can't figure out what it is. import React from 'react' import { makeStyles } from '@material-ui/core/styles' import ...

After props have been passed, the ReactJS ComponentWillMount() function is triggered

Can anyone explain why the child component is only rendered once, even though I pass props to it every time I click a button? Whenever I click a button that passes props to the child, the ComponentWillMount() method of the child component doesn't tri ...

What is the easiest way to set up a local server for deployment?

I am currently working on a research project with unique requirements that involve using private data which cannot be shared online. However, I need to create a demonstration where this data could potentially be accessed online as part of a proof of concep ...