Giter Site home page Giter Site logo

swup / body-class-plugin Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 4.0 644 KB

A swup plugin for updating the body classname 🎊

Home Page: https://swup.js.org/plugins/body-class-plugin

License: MIT License

TypeScript 100.00%
swup plugin body classname

body-class-plugin's Introduction

swup 4 is released Β πŸŽ‰Β  Check out the release notes andΒ upgradeΒ guide.


swup

npm version Bundle size npm downloads Test status License


Swup

Versatile and extensible page transition library for server-rendered websites.

Features β€’ Demos β€’ Plugins β€’ Themes β€’ Documentation β€’ Discussions

Overview

Swup adds page transitions to server-rendered websites. It manages the complete page load lifecycle and smoothly animates between the current and next page. In addition, it offers many other quality-of-life improvements like caching, smart preloading, native browser history and enhanced accessibility.

Make your site feel like a snappy single-page app β€” without any of the complexity.

Features

  • ✏️ Works out of the box with minimal markup
  • ✨ Auto-detects CSS transitions & animations for perfect timing
  • πŸ”— Updates URLs and preserves native browser history
  • πŸ“ Manages the scroll position between pages and anchor links
  • πŸš€ Uses a cache to speed up subsequent page loads
  • πŸ“‘ Offers hooks to customize and extend the page load lifecycle
  • πŸ”Œ Has a powerful plugin system and many official and third-party plugins
  • 🎨 Provides ready-to-go themes to get started quickly

Demos

Explore our interactive demos to see swup in action.

Documentation

Visit our official documentation to learn more.

Plugins

Swup is small by design. Extended features can be added via plugins:

Check out the list of official plugins and third-party integrations.

Themes

Get started quickly with one of three official themes: fade, slide, and overlay.

Examples

Take a look at the interactive demos and sites using swup for more examples.

Having trouble?

If you're having trouble implementing swup, check out the Common Issues section of the docs, look at closed issues or create a new discussion.

Want to Contribute?

We're looking for maintainers! Β  πŸ‘€

Become a sponsor on Open Collective or support development through GitHub sponsors.

This project is tested with BrowserStack.

body-class-plugin's People

Contributors

daun avatar dependabot[bot] avatar gmrchk avatar hirasso avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

body-class-plugin's Issues

Getting "TypeError: Cannot read property 'pageClass' of undefined" when using plugin

Describe your issue:
I'm trying to use this plugin in my wordpress theme, the SWUP works* but it doesn't work and returns this error:

TypeError: Cannot read property 'pageClass' of undefined
    at vendor.min.js?ver=20151215:1
    at vendor.min.js?ver=20151215:1
    at Array.forEach (<anonymous>)
    at w.e.default [as triggerEvent] (vendor.min.js?ver=20151215:1)
    at w.e.default [as renderPage] (vendor.min.js?ver=20151215:1)
    at vendor.min.js?ver=20151215:1

*I have a problem with the SWUP too which I commented here

Swup config or any additional relevant code used:

const domain = window.location.origin
const options = {    
    linkSelector: `a[href*="${domain}"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup]), a[xlink\\:href]`,
    containers: [".site"],
    plugins: [new SwupBodyClassPlugin()]
};
const swup = new Swup(options);

I also tried the config without the linkSelector change and other linkSelector configs but I still get this error.

Before creating this issue, did you think of...:

  • [-] Have you checked closed issues for similar/related problems.
  • [-] Have you provided all helpful information available?
  • [-] Have you considered creating a demo so we can help you better?

Edit:
I explained the fix here

v3.1.0 - Error: Failed to resolve entry for package "@swup/body-class-plugin".

The package may have incorrect main/module/exports specified in its package.json.

16:35:16.524 | [commonjs--resolver] Failed to resolve entry for package "@swup/body-class-plugin". The package may have incorrect main/module/exports specified in its package.json.
-- | --
16:35:16.813 | error   Failed to resolve entry for package "@swup/body-class-plugin". The package may have incorrect main/module/exports specified in its package.json.
16:35:16.813 | File:
16:35:16.813 | /opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:28708:11
16:35:16.814 | Code:
16:35:16.814 | 28707 \| function packageEntryFailure(id, details) {
16:35:16.814 | > 28708 \|     throw new Error(`Failed to resolve entry for package "${id}". ` +
16:35:16.814 | \|           ^
16:35:16.814 | 28709 \|         `The package may have incorrect main/module/exports specified in its package.json` +
16:35:16.814 | 28710 \|         (details ? ': ' + details : '.'));
16:35:16.814 | 28711 \| }
16:35:16.815 | Stacktrace:
16:35:16.815 | Error: Failed to resolve entry for package "@swup/body-class-plugin". The package may have incorrect main/module/exports specified in its package.json.
16:35:16.815 | at packageEntryFailure (file:///opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:28708:11)
16:35:16.815 | at resolvePackageEntry (file:///opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:28705:5)
16:35:16.815 | at tryNodeResolve (file:///opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:28436:20)
16:35:16.815 | at Object.resolveId (file:///opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:28197:28)
16:35:16.815 | at file:///opt/buildhome/repo/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:25356:40
16:35:16.815 | at async PluginDriver.hookFirstAndGetPlugin (file:///opt/buildhome/repo/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:25256:28)
16:35:16.815 | at async resolveId (file:///opt/buildhome/repo/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23931:26)
16:35:16.816 | at async ModuleLoader.resolveId (file:///opt/buildhome/repo/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24345:15)
16:35:16.816 | at async Object.resolveId (file:///opt/buildhome/repo/node_modules/vite/dist/node/chunks/dep-3b8eb186.js:7984:10)
16:35:16.816 | at async PluginDriver.hookFirstAndGetPlugin (file:///opt/buildhome/repo/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:25256:28)
16:35:16.816 | Β 
16:35:16.848 | Failed: build command exited with code: 1
16:35:17.750 | Failed: error occurred while running build command

This happens when deploying in an Astro website on Cloudflare. Here's an example https://github.com/MythemeCloud/mytheme-web

Not working in Safari

Description of the issue

It appears that the plugin is not working as expected in Safari (classes are not removed/added on navigation) while Firefox has no issues.

Context and environment

I tested on Safari 16.6, 17.0 and iOS 16.6.
In case this is not easily reproducible, I'll gladly create a demo.

Thank you very much for your support!

Before creating this issue...

  • Have you provided all helpful information available?
  • Have you checked closed issues for similar/related problems?
  • Have you considered creating a demo so we can help you better?

How to use exactly?

I have the plugin installed and can see it pinging the tag in Chrome Dev Tools, but it's not changing, adding, removing any classes. How exactly do I add a class based on the current page? Is there some more javascript that needs to be added besides what's shown in the example?

loadPage with query param throws error

Describe your issue:
When using loadPage() method to load an URL, the content being loaded in can be a cached version of the content, if said URL was previously visited.
To get around that I thought of invalidating the cache by appending a random GET param to the URL being loaded, each time it is called.

Something like this:

let d = new Date();
swup.loadPage({ url: url + '?r=' + d.getTime() });

But now Swup throws an error with the following message:
TypeError: page is undefined

Error happens on this line of the body class plugin:

if (page.pageClass !== '') {

When I remove the GET param, everything works as it should, albeit with cached content being returned by loadPage().

The following issues seem related:
#8
swup/swup#115

Any suggestions? Should I append the param as FormData and set the method to POST?

Before creating this issue, did you think of...:

  • Have you checked closed issues for similar/related problems.
  • Have you provided all helpful information available?
  • Have you considered creating a demo so we can help you better?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.