Using various NextJS layouts or importing CSS conditionally based on the requirements

I'm currently in the process of transitioning a legacy app to Nextjs. This app consists of two distinct layouts: one for the public facing aspect and another for the admin section. Each layout has its own set of CSS files. However, due to Nextjs limitations that only allow CSS imports in the custom _app.js file, I am unable to separate the layouts as intended. I want to avoid mixing the admin CSS with the public layout and vice versa. How should I proceed in this scenario?

I attempted to use styled-jsx, but it ended up causing more challenges. Wrapping the entire CSS file with thousands of lines using css.global caused a multitude of errors, such as not supporting nesting, among others.

Answer №1

If you want to keep your files organized, consider placing them in the /public folder and manually controlling the CSS imports. A custom _document.jsx page is a great spot for handling these included styles.

Keep in mind that this workaround allows you to separate these files from your other CSS bundles, making them accessible to the public via URL.

Answer №2

To create variation in layout components, you can utilize the dynamic function and switch between them within the _app.js file. This informative article highlights the capabilities of using dynamic.

If you encounter issues with nesting due to applying a sass/scss file from a CSS compiler within styled-jsx, consider enabling sass/scss support in styled-jsx.

_app.js

const App = ({ Component, pageProps }) => {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  )
}

Layout.js

import dynamic from 'next/dynamic';

const mapLayoutToComponent = {
  public: dynamic(import("./layouts/Public")),
  admin: dynamic(import("./layouts/Admin"))
}

const Layout = props => {
  const [layoutState, setLayoutState] = React.useState('public');
  const Component = mapLayoutNameToComponent[layoutState];

  return <Component>{props.children}</Component>
}

layouts/Public.js

import '../styles/public.css';

const Public = (props) => {
  return <>{props.children}</>
};

layouts/Admin.js

import '../styles/admin.css';

const Admin = (props) => {
  return <>{props.children}</>
};

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

Beware, search for DomNode!

I attempted to create a select menu using material-ui and React const SelectLevelButton = forwardRef((props, ref) => { const [stateLevel, setStateLevel] = useState({ level: "Easy" }); const [stateMenu, setStateMenu] = useState({ isOpen ...

Issue with OnClientClick functionality not functioning as expected

I am having trouble with the validation function that is supposed to be triggered when clicking on the back and next buttons in my code. For some reason, the OnClientClick validation function is not being called when I click on the buttons. Can anyone pro ...

What is the best way to send parameters to a callback function in a jQuery $.getJSON method?

Currently, I am attempting to utilize jQuery to access a custom API using Ajax/$.getJSON. In my code, I am trying to send a specific value to the Ajax callback method, but I am encountering an issue where this value is not being properly passed and instea ...

The directive in an ionicModal is executed first, even before the controller is fully loaded

I recently encountered a challenge with a $ionicModal containing a directive called <scrap-link-cards>, which accepts a two-way binding scope object. This directive is included in the $ionicModal template: <scrap-link-cards datasource=(updates.up ...

Include a parent class within the style tags in your CSS code

Currently, I am facing an issue with a web application that I am developing. On the left side of the page, there is an editable area, and on the right side, there is a live preview. The live preview area contains an HTML file with specific fields that can ...

Is there a way to change a .pptx document into a base64 string?

Currently, I am working on a project that involves creating an addin for Office. The challenge I am facing is opening other pptx files from within the addin. After some research, I discovered that I need to use base64 for the PowerPoint.createPresentation( ...

The art of creating drop shadows in Three.js

Is there a method to create a shadow without needing a "ground" plane to project it on? I prefer this option so that when the camera moves around the object, it doesn't appear as though the shadow is passing through the ground. I am currently utilizi ...

When clicking on the side-bar, it does not respond as expected

My website has a menu layout that features a logo on the left and an icon for the menu on the right side. When the icon is clicked, the menu slides in from the right side of the window, and when clicked again, it slides out. However, I am facing two issues ...

Here's a guide on how to display texts underneath icons in Buttons using Material UI

Currently, this is the Button I have displayed https://i.sstatic.net/YkHp0.png I am trying to figure out how to position the Dummy Button text beneath the icon. Can someone assist me with this? Below is my code snippet: <Button className={classes.d ...

XML integration with Jplayer is not functioning properly

I'm in the process of setting up Jplayer to work with an XML document so that my client can easily update the playlist. I managed to get it working flawlessly when I manually coded the songs into the JS. Here's my current code: <div class="bo ...

How does the object scope receive, process, and prepare the update for display within the AngularJs view?

AngularJS allows for data-binding to easily display immediate data in our View. This is made possible by the object scope, which acts as a connector between the Logic Code and The View. Additionally, AngularJs supports two-way binding. My question is: H ...

How can I showcase the index of `<tr>` and `<td>` elements in a dynamically generated table

How can I print the index of table rows and data on click in javascript? <html> <head> <title>Table Creation</title> <script language="javascript" type="text/javascript"> function createTable() { ...

Choose and adjust the size of images with a specific aspect ratio using jQuery

As I work on my Tumblr blog, I am experimenting with using jQuery to target photo entries (imgs) based on specific aspect ratios and resizing them accordingly. To be more precise: If an image's width exceeds 250px, it should be resized so that the h ...

Proper method for executing a synchronous AJAX POST request and subsequently submitting a form

Currently, I have an ASP submit button in my code. <asp:button runat="server" Text="Submit" id="btnSubmitOrder" OnClientClick="return SubmitOrder()" /> The JavaScript function I'm using, SubmitOrder(), is designed to execute a POST request. De ...

How can I resize an element using jQuery resizable and then revert it back to its original size with a button click?

I need help figuring out how to revert an element back to its original size after it has been modified with .resizable. I attempted the following: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code. ...

Accessing the user's referral URL in Express.js when they submit a post

I am looking to implement a commenting system on my website, and I need to capture the page information where the comment is being made (such as a specific video uploaded by a user) in order to save it in the database. The route structure is set up like t ...

NestJS WebSocketGateway fails to initialize due to a technical glitch

After following the instructions in the NestJS documentation, I set up the websockets gateway within the AppModule. The server is starting without any issues, and I'm able to serve static assets via HTTP successfully. However, I'm facing difficul ...

Having trouble updating text in a PDF using NodeJS and hummus library

How can NodeJS be used to replace a string in a PDF file? offers a solution for text replacement in a PDF document. However, when applying the same code, an issue arises where the replaced text is visible in the source code of the PDF but does not render p ...

Error: Node.js Express is unable to recognize the content type

I'm currently facing an issue while trying to transmit a file from a phonegap application to a nodejs server, specifically with regards to the content-type in the request headers. Error: unrecognized content-type: multipart/form-data;boundary express ...

The process of verifying email addresses using Angular 5

I'm having trouble validating my email with the .com domain. I've tried various methods, but so far, none have worked. Can anyone provide guidance? I'm new to Angular and struggling with this particular issue. Ideally, I want the email to be ...