Creating a collapsible element in reactjs using the sleek styling of tailwind css

I am currently working on creating a collapsible section component design using React JS with TailwindCSS. The component includes an edit button to toggle the information section, positioned next to the input field. Although I have completed the initial design (as seen in screenshot 1), some adjustments are required in the styling. This has led to confusion regarding how to integrate the edit info button and info section code within a single component to achieve the desired layout shown in the screenshot.

    <div>
      <div
        className={[
          'flex',
          'justify-between',
          'relative',
          'lg:px-20',
          'xl:px-40',
          'py-6',
        ].join(' ')}
      >
        <div className="flex items-center sm:flex-col lg:flex-row">
          <div className="flex flex-col lg:flex-row md:flex-row items-center">
            <StreamTokenInputField />
//EDIT INFO BUTTON
            <button
              className="button"
              onClick={() => setIsCollapseTrue(!isCollapseTrue)}
            >
              {i18n.t(Edit Info)}
              {isCollapseTrue ? (
                <IoIosArrowUp className="font-extrabold ml-2 text-lg" />
              ) : (
                <FiChevronDown className="font-extrabold ml-2 text-lg" />
              )}
            </button>
          </div>
        </div>
      </div>
// INFO SECTION
      <div
        className={[
          'container',
          'mx-auto',
          'md:w-full',
          'w-96',
          'py-8',
          'my-4',
          'lg:py-20',
          'lg:px-40',
          'bg-skin-card',
          'rounded-3xl',
          !isCollapseTrue && 'hidden',
        ].join(' ')}
      >
        {/* OTHER CODES */}
      </div>

    </div>

screenshot 1 https://i.sstatic.net/P1OFz.png

My attempt so far:

      <div
        className={[
          'flex',
          'justify-between',
          'relative',
          'lg:px-20',
          'xl:px-40',
          'py-6',
        ].join(' ')}
      >
        <div className="flex items-center sm:flex-col lg:flex-row ">
          <div className="flex flex-col lg:flex-row md:flex-row items-center">
            <StreamTokenInputField />
            <button
              className={[
                'flex',
                'button',
                'button-green',
                'xl:px-6',
                'md:px-2',
                'lg:px-10',
                'lg:my-6',
                'md:my-6',
                'mx-4',
                'justify-center',
                'uppercase',
                'font-semibold',
              ].join(' ')}
              onClick={() => setIsCollapseTrue(!isCollapseTrue)}
            >
              {i18n.t(buttonName)}
              {isCollapseTrue ? (
                <IoIosArrowUp className="font-extrabold ml-2 text-lg" />
              ) : (
                <FiChevronDown className="font-extrabold ml-2 text-lg" />
              )}
            </button>
            <div
              className={[
                'container',
                'mx-auto',
                'md:w-full',
                'w-96',
                'py-8',
                'my-4',
                'lg:py-20',
                'lg:px-40',
                'bg-skin-card',
                'rounded-3xl',
                !isCollapseTrue && 'hidden',
              ].join(' ')}
            >
              {/* OTHER CODES */}
            </div>
          </div>
        </div>
      </div>

However, the output is not as intended: I aim for the info section to be displayed below the edit info button, matching the screenshot provided above.

https://i.sstatic.net/OULJ9.png

Answer №1

In order to have the information section appear below the button, you do not need to use lg:flex-row. Your code should look like this:

<div className="flex items-center flex-col">
[...]
</div>

It is also worth noting that Tailwind CSS follows a mobile-first breakpoint system, so there is no need to explicitly add sm: to sm:flex-col.

For example, in this line of code

<div className="flex flex-col lg:flex-row md:flex-row items-center">
, once you have included md:flex-row, there is no need to include lg:flex-row. Any larger size than md will automatically apply the same styling as defined for md:.

For further details on Tailwind's mobile-first breakpoint system, please refer to mobile-first-breakpoint-system

Answer №2

If you need to add some flair to your webpage, consider using the detail tag. Feel free to customize the animation or even swap out the default icon for a more unique one.

<details className="open:bg-white border-b  open:ring-1 open:ring-black/5 open:shadow-lg p-6 rounded-lg transform-gpu delay-75 duration-100 ease-in-out ">
   <summary className="leading-6 text-slate-900 dark:text-white font-semibold select-none">
       Have you ever wondered why Ovaltine is called Ovaltine?
   </summary>
   <div className="mt-3 text-sm leading-6 text-slate-600 dark:text-slate-400">
       <p>Think about it - the mug is round, the jar is round...shouldn't they be calling it Roundtine?</p>
   </div>
</details>

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

What steps can be taken to stop the grandchild with an absolute position from increasing the scrollHeight or scrollWidth of containers?

Is it possible to make a container scroll size dependent on its child only? The issue arises when the child element (.scrollable) contains absolutely positioned elements that may exceed the boundaries of the child. How can I make sure that the containers ...

The navigation bar in React Router is interfering with the loading of other components

Currently, I am in the process of setting up a simple navigation bar that consists of basic buttons without any complex functionality. However, I have encountered an issue where placing the navbar inside the switch prevents other components from loading ...

transferring data from grails to javascript via a map

I am facing an issue with passing a Map object from my Grails controller to JavaScript. The code snippet in my controller is as follows: def scoreValue = new HashMap<String,String>(); scoreValue.put("0","poor"); scoreValue.put("1","good"); ... retu ...

The issue of the "port" attribute not working for remotePatterns in the Image component has been identified in Next.js 13's next.config.js

I've encountered an issue with the code snippet below. I'm attempting to utilize remotePatterns in my next.config.js file to enable external images. Strangely, when I set the port to an empty string "", it functions correctly. However, specifying ...

It is not possible to invoke a function within the mounted() lifecycle hook

I recently integrated a chat API into my Vue.js project for connecting to chat rooms. Upon entering a chat room page, an ajax request is triggered which then calls a function responsible for fetching the complete chat history: mounted() { $.ajax({ url: ...

Emphasize a passage by clicking on a different section of text

Seeking Assistance I am currently customizing this template which incorporates the MixItUp plugin. My query pertains to highlighting the "filter tab" upon clicking on the corresponding text when hovering over each image, a task I find challenging as a new ...

Issue encountered: The differ cannot recognize the provided object '[object Object]', which is of type 'object'. NgFor is limited to binding Iterables only

I have been following a tutorial on Ionic created by Paul Halliday, focusing on a shopping list project that utilizes Firebase and Angular. However, I am encountering an error every time I try to run the application: Error: Uncaught (in promise): Error: ...

How can TypeScript and MobX work together using Set()?

I'm encountering errors while trying to implement Set() in my Grocery Shopping app using MobX with TypeScript. I have a simple setup with defined types in types.ts: types.ts export type Item = { name: string; instock: number; price: number; }; ...

Updating the geometry of an object while the camera is in motion can result in visual errors in the three.js library

I am currently facing an issue with updating a vertex of a line in my three.js project. My goal is to create a line within my scene where one end is fixed at (0,0,0) and the other end is set to a specific position on the user's screen (x,y coordinate ...

The nested grid containers and items are overflowing off the screen when using material UI react

I am attempting to create a user interface similar to the one shown below, but I am facing some difficulties in achieving the desired outcome. I have experimented with adjusting the columns as well, but that didn't yield the expected results either. ...

Using Vue Axios to load a Laravel view can be a seamless process that

I'm attempting to create a hyperlink to a Laravel view named faq.blade.php from my Vue component. I've tried using axios, and even though it logs the response in the console, the view isn't loading. How can I resolve this issue? FaqControll ...

Why do my checkboxes refuse to respond separately?

I am currently experiencing an issue with the checkboxes in my JavaScript game. They do not respond to individual clicks as expected. Each time I log the output, I receive index 0 regardless of which box I click on. The checkboxes seem unresponsive but the ...

What is the best way to eliminate backslash escaping from a JavaScript variable?

I am working with a variable called x. var x = "<div class=\\\"abcdef\\\">"; The value of x is <div class=\"abcdef\"> However, I want it to be <div class="abcdef"> Can someone help me unescape ...

Updating database through AJAX calls does not work

I'm currently developing a system that requires the ability to remove employees from their Saturday shifts. This process involves clicking on an icon that triggers the JavaScript function "removeEmpFromSaturday" and passes relevant parameters. Within ...

Is Bootstrap the antithesis of collapse?

I am fairly new to working with Bootstrap. While I have a script that functions, I am struggling to make modifications to it. The script has two main functionalities: 1) Clicking on a header will toggle the visibility of the following div. 2) Clicking o ...

I'm having issues with my JS Accordion, it seems to be behaving erratically and jumping around unexpectedly

Struggling to implement a JavaScript accordion I came across, and encountering some issues. The expected behavior is for the titles to smoothly slide up to the top of the page and reveal the content below. However, there seems to be some noticeable jerkine ...

Implementing a Reset Button to Clear Checkboxes with PHP or JavaScript

I'm looking for help with setting up a button to reset the checkboxes on my preferences page. UPDATEL: Here's more information: The solutions provided should only affect checkboxes that are currently selected, not those enabled onLoad. This is ...

What is the best way to generate a new Object using an Object that contains Arrays?

I currently have a global array saved with a catalog and a list of items that the user has saved. My task is to generate a new array of Objects (with arrays) containing only the items saved by the user. I am working with javascript in react-native, and I ...

Javascript code for replacing <br> tags with line breaks in a string

Currently in the process of utilizing react to access comments stored in an api, however, these comments contain multiple instances of br tags due to them originating from a php user. The task at hand is to replace these br tags within each string with act ...

Is there a different option for determining the space between the main body and footer instead of using a

After creating a footer that sticks to the bottom of my webpage, I noticed that it lines up right after the last image with no spacing in between. To add some space between the final image and the footer, I considered using a percentage-based margin. Howev ...