I am making a portfolio website with nuxt3, tailwindcss and the WordPress REST API.
The general layout of the portfolio website is as follows:
<body>
<div id="page-wrapper">
<section id="landing-image" class="w-screen h-screen">...</section>
<section id="about">...</section>
<section id="projects-grid">...</section>
</div>
</body>
Goal:
I want the website to feel like it is only one page, but support clicking on a project from the project grid to read more about the project. When clicking on a project, the content should load without a (visible) reload of the page. See this portfolio site as an example of the project grid with project popup that I try to achieve: https://www.tessarosejacksoncomposer.com/
What I have tried:
1.) One way I thought of doing this, is by adding a div to the page that contains the post content which is hidden by tailwind and nuxt keeps track of the state. Nuxt conditionally sets the ‘hidden’ class when the post needs to be seen or not and the the correct project content is loaded.
app.vue
<template>
<div id="page-wrapper">
<section id="landing-image" class="w-screen h-screen">...</section>
<section id="about">...</section>
<section id="projects-grid">...</section>
</div>
<div id="project-content-wrapper" class="absolute h-screen w-screen" :class="{'hidden': postHidden }">
... (dynamically load project content and unhide by nuxt state management) ...
</div>
</template>
This is a nice and simple implementation, but my main problem with it is that each project does not have a unique url. When I want to share a specific project, I want to send a url that opens the project imediatly. Now the url always stays on the home page. Second problem with this is that there is no url history when using the site, which I think is not intuitive.
2.) So, I found a second way of doing this using nested nuxt pages and the <NuxtPage />
component (reference: https://v2.nuxt.com/examples/routing/nested-pages/ ). My idea was to put the <NuxtPage />
tag in the project content div and then have a nested page that changes based on the url, but does not reload the entire page (visibly). In the pages/
directory I created a project/
directory, project.vue
file and in the project/
directory I added a [...slug].vue
file that contains the project layout and renders the content. This way my projects have the url example.com/project/project-slug
. The project.vue
file is only a passthrough to get a nice looking url and contains only a template with a <NuxtPage />
to nest the project children.
app.vue
<template>
<div id="page-wrapper">
<section id="landing-image" class="w-screen h-screen">...</section>
<section id="about">...</section>
<section id="projects-grid">...</section>
</div>
<div id="project-content-wrapper" class="absolute h-screen w-screen" :class="{'hidden': postHidden }">
<NuxtPage />
</div>
</template>
This second method solved the url issue, but the page visibly reloads and breaks the single page effect. That is because the page can scroll and when going to a project using <NuxtLink>
, the site scrolls to the top. This becomes a problem when I add a cross UI element for closing the project pop up and going back to the home page. When clicking on the cross, then the page is on top, forcing the user to scroll back down again to the project grid. I want the illusion that the popup opens over the site and when clicking on the cross, the user continues where they left off.
Question:
Does anyone know a way to have achieve this project grid popup effect with unique urls for every project while maintaining the feeling of staying on the same single page?
2
Answers
The suggestions of @lousolverson and @Mojtaba, and a lucky find of the term 'Modal' instead of what I called a 'popup', send me down a rabbit hole and I eventually was able to get the behavior that I want.
My solution builds further on my 1st attempt , but using the
navigateTo
composable of Nuxt3. This composable is a nicer way to programmatically change the url than thewindow.history.pushstate
and also does not reload the page on url change.The state of the postmodal is stored using the useState composable, which is false if the user is on the home page with the projectsgrid and the modal is closed. Then if the user clicks on a ProjectCard, then this state changes to the slug of the selected project and the projectModal will open with the content of the selected project.
If someone gets a url that directly leads to a project, then the
onMounted()
function will set the correct state and scroll the underlying page to the projectsgrid. Also, the url history is preserved. So, the site will give a fluent experience without page reloads, but supporting unique urls for each project.Thank you for the suggestions and I hope someone else may find this useful! :)
app.vue
components/ProjectCard.vue
components/ProjectModal.vue
You could try something like this (nota: you should have one root element):
This is a crude implementation that for example does not support errors. The idea is to use ‘window.history.pushState’ to modify the url with a query string and use this query string, if it exists, when the page loads