Create a landing page with Next and MDX
I recently received Colby Fayocks 50reactprojects and thought it would be a fun idea to run through some of the example projects as tutorials, while putting a little spin on them. So we begin with project 1 of the 50 (well, why not!), the marketing website. Colby does a great job of listing out the requirements of the project and some of the tech you could use to get it made.
This tutorial assumes some prior knowledge of modern frontend development but will safely guide you through the whole setup and development process.
Helpful to know
The marketing team for the company you work for just launched a new product. They need a detailed marketing plan that includes a website and landing page dedicated to driving people to learn about the product and ultimately purchase it.
Well that sounds clear and concise. We are to create a landing page and website that will entice the user to purchase the product. For our example we'll skip the website creation and concentrate on creating a killer landing page. For our stack we will keep it modern and use NextJS and MDX. We will style our site with the wonderful Chakra-UI.
We will also go over how to create a page from a design and dive into a little CSS grid. Oh you lucky lot!
Install the stuff
We'll use create-next-app to bootstrap our project and get the basics installed. Run the following command and input the projects name. I'll choose next-mdx-landing-page, because Im feeling super imaginative but feel free to go wild and name it anything you like.
Next we are going to install two more packages to handle MDX.
Once they have been installed open the project in your editor of choice, Im using VSCode. You'll see that an api folder was installed along with an example serverless function. We will not be using that so you can go ahead and delete the whole api folder. In fact, you can also remove the styles folder and all of its content, the index.js file under the pages folder and the favicon.ico and vercel.svg inside of the public folder.
So to recap, remove the following from the project:
We'll keep the public folder incase we want to store some images later on. Now our app is going to need just one page, a landing page. That page is going to be an MDX file. So go ahead and create a new index.mdx inside the pages folder. Then open up _app.js, this is the root of our site, it's from here that we will wrap the app and provide our MDX file its components. For now lets wrap the base component with the MDXProvider.
There is no need to import React as it's globally available to us. Open up the index.mdx file and write the traditional "MDX is amazing". Now for Next to be able to pick up our MDX file located in the pages directory we will have to configure it to do so. At the projects root create a new file called next.config.js. In here we will require MDX and set the page extensions to look for MDX files in the pages directory.
Now we can run
yarn dev, Next will spin up a dev server for us on port 3000, and if everything went well we should see the contents of our index.mdx file - MDX is amazing!
Get some styling in there
Cool beans, our project works and our app loads an MDX file. But it's nothing to look at at the moment. Let's add Chakra-UI. We'll be using v1.0 so be sure to consult the documentation for that version (which at the time of writing is the latest) if you run into any troubles with Chakra.
As a brief overview of the installation differences, < v1.0 you had to install
This has now been consolidated down to one package, which is great. Run the following to install Chakra. The version that installs with this tutorial is 1.0.0-rc.3.
Once Chakra is installed wrap the app with the ChakraProvider just like the MDXProvider in _app.js. This will enable us to later use the components we set to the markdown elements within our MDX file.
Now that we have Chakra hooked up let's add a base theme. Out of the box Chakra comes with some great defaults for colors inspired by Tailwind CSS, but as a way of learning how to do it we can add our own colors. I like to explore coolors.co when trying to find inspiration for website color palette. In the top right click explore and find a palette that you like. Once you have decided on your palette click the 3 dots and open in generator. Once there you can click view shades on each color and choose some different shades, getting lighter from the original color (so 4 steps upward until you have 5 shades including the original) These will come in handy when adding depth to elements.
Create a theme.js file at the projects root. This will enable us to house our sites styles in one place and easily import and use them in our Chakra components.
For the sites font, I went with Roboto from Google fonts. Feel free to change this. Using the Chakra global styles object we can easily set the font for the whole site in the html and body tag. If we were to have two fonts, say for our headers and body, then another approach could be to use the fonts object key in the theme and set our different fonts. Then these would be accessed via the fontFamily key on the Chakra components.
Which ever way we choose to use our fonts we must first set them so that our site can use them. The easiest way to do this is by simply adding a link tag into the sites html head. You could do this on every page (assuming you had many pages in your site) but next gives us a nice solution which enables us to override one file and insert our link there. In our pages directory create a new file called _document.js. This is a custom document used to augment the sites html and body tags. From here we can insert a script or link into the head and it will be applied to every page in our site. In our case we are going to insert our link tag from Google fonts.
Going back to our _app.js file we can now import and pass in our new theme file so that Chakra knows that it exists.
Learnt so far...
MDX is just markdown that can render React components. That means that as well as using typical markdown syntax such as the pounds for titles and dashes for lists we can map custom components to these default markdown elements, enabling us to personalise our file while at the same time writing it in standard markdown syntax. Lets add use some of the Chakra components and map them to our markdown elements and pass them into the MDXProvider.
As you can see from the above object, we map the Chakra component to an object key that represents a markdown (well, a html element that is mapped in markdown to certain symbols) element. Now when we write markdown the h1, h2, h3, p, ul and li will map to those from Chakra and in turn conform to our theme. One more special thing to note here is that we can pass in custom React components (Like the Header, HeaderText and Section. Don't worry, we'll create those in a sec) and they will be available to us in the MDX file without having to import them. This is where some of the power of MDX comes into play. We can create anything we want, be it a chart to display data or a simple box to display text in a certain format. Take this simple example, a component I wrote to display the Prerequisites for my tutorial pieces (You would have encountered it as you entered this page).
Which is then used in this MDX file like below. Of course, this is a very simple component and one I will continue to work on but it gives you an idea for what is possible with MDX.
Our landing page has got to draw the attention of the general public, our clients products fate is in our hands! For this kind of task, with no layout brief supplied I like to dive into the world of posters. Posters are a great resource for getting layout ideas. Posters are designed to provide as much information as possible to the viewer while still be pleasing on the eye so they engage the viewer long enough for them to soak up the message. One such site which is filled with awesome designs is swissted.com.
Now lets for a moment assume that our clients company name is Bad Design. I know, I know, but bear with me. This will play quite nicely with our choice of design, not to mention its a bit tongue in cheek! Taking the Bad Religion poster we can replace the words Bad Religion with Bad Design. This poster also gives us a nice chance to play around with CSS grid implementations and how we would do that in an MDX file!
You'll notice that the colors are very simple, we have an orange, a white and a black. If we add our clients info at the top of the page the same as the poster we can logically say they our grid will have 3 columns and 5 rows. This should be a fairly straight forward layout to recreate, we'll use the Grid component exported from Chakra which comes with some nice shorthand props. I took the liberty of using an Eye Dropper tool (A chrome extension) to grab the exact orange, black and white shades. Lets add those to our theme.js file.
Sweet, now that we have our colors lets get started with our components. Inside the components folder add 4 new components.
These will form the basis for our landing page. We will be splitting up our components and making them an generic as possible so that we can reuse them if we choose to add more pages to the site further down the line. Starting with the layout, and keeping one eye on our design, lets create our grid.
We take advantage of the Chakra Grid component and pass in any children, these will amount to the columns and rows. Our page will be responsive so and look like a poster. We have used percentages for the rows as this gives us some flexibility when the viewport changes size. Our 3 columns are set to take up all available space. We set a grid gap and the background color to our grey/white, that way the gaps appear to actually be the background of the page. By giving the grid a max width of 1000px we are keeping to the poster format and our classic margin trick of vertical 0, horizontal auto centers the whole thing.
Moving on to our header component then.
This will be the black strip that runs along the top of the page and houses 3 pieces of text. Chakras Flex component is basically just a div with some nice flex like defaults, for example, if we don't specify it will default to flex-direction="row". Another nice touch that should be utilized when using Chakra is the as prop. With this we can keep our components and in turn our HTML semantic. Our header is in fact a header so we set it as so. The grid row is quite obvious. The grid column however may look a little strange but really its quite simple. We are saying that this component should start at column 1 and end at the last columns edge. Much like if we were to grab the last element form an array, -1 tells grid that we want the end.
Our header component accepts children, those children will be our next component, the HeaderText.
This component does exactly what it says on the tin. That is, it just displays its children in a Chakra Text component. Here we utilize the responsive array syntax so that depending on the viewports size, our text will shrink or grow. We can specify these breakpoints in our theme file using the breakpoints key like so:
But this is unnecessary for our use case and we can just accept the defaults. (Which happen to be the above) In the responsive array syntax, if we skip an index or pass in null then it will be ignored. In our case we make a broad assumption that for mobile we'll use md, tablet 1xl and desktop 2xl.
Our last component is what will section up our content and set our elements in place for the grid to work its magic.
Here we are creating a flexible section, setting its background color to the posters orange, passing in children which in turn are passed to a Text component and then spreading the rest of the props that may or may not be added later on. In fact we will use the rest to tell this component where in the grid it should go.
So now that we have all of our pieces lets put them together and see how simple it can be to re-create that poster! Head on over to the index.mdx file you created at the beginning of this tutorial and add the following:
Learnt so far...
Its not a perfect match to the poster but I believe that our client would be mightily impressed with our work!
This post is a work in progress and I may add more parts to it as and when I find the time. Im sure our client would like to display some images of their super amazing new product after all, but we will leave that for another day. Thanks for reading, I'll post on Twitter if and when I add more content to this!