1.1.0 • Published 1 year ago

astro-base-document v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

🚀  Astro — Base document enclosure

NPM Downloads ISC License PRs Welcome

Astro TypeScript Prettier EditorConfig ESLint

Provide sensible defaults for HTML base document.
The goal is to reduce boilerplate when using multiple layouts in Astro projects.



📦  Installation

pnpm i astro-base-document

🛠  Usage

In your layout component, for example ./src/layouts/Default.astro:

---
import { Document, Body } from 'astro-base-document';

// ...
---

<!-- All properties are optional -->
<Document
  id="test-base-rootDoc"
  data-theme="default"
  lang="en"
  class:list={[`mode-${import.meta.env.MODE}`, 'my-class-html']}
>
  <Fragment slot="head">
    <!-- Things you can inject in `HEAD` -->
    <style is:inline>
      .some-class {
        color: red;
      }
    </style>
  </Fragment>

  <Body slot="body" id="my-id" class:list={['my-class-body']}>
    <!-- Things you can inject in `BODY` -->
    <nav>My navbar</nav>

    <!-- Your page content slot  -->
    <slot />
  </Body>
</Document>

🎉  Result

<!DOCTYPE html>
<html lang="es" class="some-class doby-doba js" id="the-id">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <script>
      // CRITICAL
      // Add `js` class to `HTML` tag if JavaScript is enabled
      (() => {
        const root = document.documentElement;
        root.classList.add('js');
      })();
    </script>

    <style is:inline>
      .some-class {
        color: red;
      }
    </style>
  </head>

  <body class="other-class dabadi-dabada" id="much-id">
    <nav>My navbar</nav>

    <!-- Page content slot -->
    <div>My page content</div>
  </body>
</html>

The js class is added to HTML tag, critically.
That way, you can handle CSS for users with or without JS enabled with .js & / :not(.js) & SCSS selectors.

💈  Interface

/// <reference types="astro/astro-jsx" />

export interface Props extends astroHTML.JSX.HTMLAttributes {
  /**
   * **Default**: `'en'`
   * */
  lang?: string;

  /**
   * Provides early JS detection for further CSS use
   * by adding `js` class critically to HTML element.
   *
   * **Default**: `true`
   * */
  jsEnabledClass?: boolean;

  /**
   * **Default**: `null`
   * */
  redirectTo?: string | null;
}

LIVE DEMO  🎭  DOCUMENTATION WEBSITE ⎋

Live demo website

code.juliancataldo.com


Other projects:


🔗  JulianCataldo.com

1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago