@aqzhyi/astro-react-mantine

0.1.2 • Public • Published

@aqzhyi/astro-react-mantine

Although it may go against Astro's original purpose, you still want to use mantine.dev with Astro

The main reason why Astro cannot work with mantine.dev is that Astro does not share the React Context. this makes the MantineProvider not usable and causes the page to crash.

[!TIP]

for example

When your astro parts are set up like this,

<BasicLayout>
  <MantineProvider
    defaultColorScheme='dark'
    client:only='react'
  >
    <Content client:load />
  </MantineProvider>
</BasicLayout>

[!IMPORTANT]

Will receive an error message:

ERROR: @mantine/core: MantineProvider was not found in component tree, make sure you have it in your app


Current Solution

Use a specific React component as the main part to give the shared features of ReactContext, and show it as the only main part in Astro with client:load

[!CAUTION]

It's Working, as pseudocode below:

//
// Create a React component that integrates MantineProvider as the root component for an Astro island component
export const createAstroComponent = (config: { component: React.ComponentType<React.PropsWithChildren> }) => {
  function MantineContextContainer() {
    return (
      <MantineProvider defaultColorScheme='dark'>
        <config.component />
      </MantineProvider>
    )
  }

  return MantineContextContainer
}

//
// Create your Mantine interactive component layout and export it for Astro.
export const DemoContainer = createAstroComponent((props: React.PropsWithChildren) => {
  return (props: React.PropsWithChildren) => {
    const [title, setTitle] = useState('')
    const [items, setItems] = useState<string[]>(['111', '222', '333', '444', '555', '666'])

    return (
      <SimpleGrid>
        <TextInput
          value={title}
          onChange={(current) => {
            setTitle(current.target.value)
          }}
          label='Add a task to the to-do list'
        />
        <Button
          color='orange'
          onClick={() => {
            setItems((prev) => ([...prev, title]))
            setTitle(() => '')
          }}
        >
          Add
        </Button>
      </SimpleGrid>
    )
  })
}

Installation

pnpm i @aqzhyi/astro-react-mantine -S

Usage

[!NOTE]

import <MantineLayout /> in ./src/pages/_MyLayout.astro

---
import { Head, MantineLayout } from '@aqzhyi/astro-react-mantine'
---

<MantineLayout
  lang='zh-TW'
  {...Astro.props}
>
  <Head>
    <meta
      name='keywords'
      content={'aqzhyi, github, library, packages, docs, astro, react, mantine, layout, ui'}
    />
  </Head>

  <slot />
</MantineLayout>

Package Sidebar

Install

npm i @aqzhyi/astro-react-mantine

Weekly Downloads

6

Version

0.1.2

License

MIT

Unpacked Size

11.9 kB

Total Files

15

Last publish

Collaborators

  • pleasurazy