fresh icon indicating copy to clipboard operation
fresh copied to clipboard

Using Material UI / Preact compatible component library

Open AravindPrabhs opened this issue 4 years ago • 12 comments

Hi,

I was trying to include Material UI since it is compatible with preact: https://twitter.com/preactjs/status/1152267975078154240?lang=en-GB

And including MUI core as a dependency as in normal deno: https://github.com/denoland/deno/issues/7282

However I run into [object Object] is not a valid HTML tag name: https://github.com/denoland/deno/issues/7282 when doing it both as a route and as an island.

Would in general all the packages in https://preactjs.com/about/libraries-addons/ technically be usable with Fresh ?

AravindPrabhs avatar Jul 04 '22 16:07 AravindPrabhs

https://mui.com/material-ui/guides/server-rendering/ I think MUI need some setup for server side rendering like the twind in fresh demo

raymclee avatar Jul 04 '22 23:07 raymclee

To my understanding this extra configuration is a bonus to make sure there is no flickering since we re-render on the client side - so I assume purely using an island should also work.

However, I did give this a go, but this feels very non-standard and I was unsure how to inject html and css. I end-up getting React not defined error.

import { h } from "preact";
import {CssBaseline} from '@mui';
import { ThemeProvider } from 'mui/styles';
import createCache from 'https://esm.sh/@emotion/cache';
import { CacheProvider } from 'https://esm.sh/@emotion/react';
import createEmotionServer from 'https://esm.sh/@emotion/server/create-instance';
import render from 'preact-render-to-string';
import theme from './theme.ts';

export function createEmotionCache() {
  return createCache({ key: 'css' });
}

export function serverMUI(App: any) {
    const cache = createEmotionCache();
    const { extractCriticalToChunks, constructStyleTagsFromChunks } =
        createEmotionServer(cache);

    // Render the component to a string.
    const html: string = render(
        <CacheProvider value={cache}>
            <ThemeProvider theme={theme}>
                {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
                <CssBaseline />
                <App />
            </ThemeProvider>
        </CacheProvider>
    );

    // Grab the CSS from emotion
    const emotionChunks = extractCriticalToChunks(html);
    const emotionCss: string = constructStyleTagsFromChunks(emotionChunks);

    return <div dangerouslySetInnerHTML={{__html: emotionCss.concat(html)}}/>
}
export * from "mui";

For any component that I want rendered with MUI I just wrap it in a serverMUI call. Note that replicating the theming and caching and an island gets the same exact error - the previous error described happens if you remove the wrapping CacheProvider etc.

AravindPrabhs avatar Jul 05 '22 09:07 AravindPrabhs

Actually I realise the better way to do the above is using the request handlers instead but I still get the same error.

AravindPrabhs avatar Jul 05 '22 10:07 AravindPrabhs

maybe try adding "react": "preact " in import_map ?

Actually I realise the better way to do the above is using the request handlers instead but I still get the same error.

raymclee avatar Jul 05 '22 11:07 raymclee

that isn't a valid map

AravindPrabhs avatar Jul 05 '22 13:07 AravindPrabhs

The approach here might work for you.

tylrw avatar Jul 05 '22 19:07 tylrw

I got it to work by adding this to the import_map.json. (I tested with this example from MUI's Website and I got an alert in the browser) "@mui/material": "https://esm.sh/@mui/[email protected]?alias=react:preact/compat,react/jsx-runtime:preact/compat/jsx-runtime&[email protected]"

SalvadorLekan avatar Jul 07 '22 11:07 SalvadorLekan

Thanks guys that works well !

AravindPrabhs avatar Jul 08 '22 07:07 AravindPrabhs

Actually I realise the better way to do the above is using the request handlers instead but I still get the same error.

Can you share your final working code? I also want to implement Material UI. I tried using a request handler inside routes/index.tsx but I'm getting the following error:

An error occured during route handling or page rendering. TypeError: Cannot read properties of undefined (reading 'context')
    at Module.w (https://esm.sh/v86/[email protected]/X-YS9yZWFjdC9qc3gtcnVudGltZTpwcmVhY3QvY29tcGF0L2pzeC1ydW50aW1lLHJlYWN0OnByZWFjdC9jb21wYXQ/deno/hooks.js:2:1132)

marsidev avatar Jul 11 '22 21:07 marsidev

@marsidev I had the same issue, updating the preact deps version in the @mui/material import line to the same than fresh is using 10.11.0 fixed the issue for me, example.

{
  "imports": {
    "$fresh/": "https://deno.land/x/[email protected]/",
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/",
    "preact-render-to-string": "https://esm.sh/*[email protected]",
    "@preact/signals": "https://esm.sh/*@preact/[email protected]",
    "@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
    "twind": "https://esm.sh/[email protected]",
    "twind/": "https://esm.sh/[email protected]/",
    "@mui/material": "https://esm.sh/@mui/material@^5.10.17?alias=react:preact/compat,react/jsx-runtime:preact/compat/jsx-runtime&[email protected]"
  }
}

gbahamondezc avatar Dec 10 '22 21:12 gbahamondezc

see also: #363

reosablo avatar Feb 04 '23 08:02 reosablo

my deno.json/imports update that works with Fresh 2: "imports": { "fresh": "jsr:@fresh/core@^2.0.0-alpha.34", "@fresh/plugin-tailwind": "jsr:@fresh/plugin-tailwind@^0.0.1-alpha.7", "preact": "npm:preact@^10.26.9", "@preact/signals": "npm:@preact/signals@^2.0.4", "tailwindcss": "npm:tailwindcss@^3.4.3", "@mui/styled-engine": "npm:@mui/styled-engine@^7.2.0", "@mui/icons-material": "https://esm.sh/v128/@mui/[email protected]?alias=react:preact/compat,@types/react:preact/compat,react-dom:preact/compat&external=preact", "@mui/material": "https://esm.sh/v128/@mui/[email protected]?alias=react:preact/compat,@types/react:preact/compat,react-dom:preact/compat&external=preact" },

emotilla avatar Jul 13 '25 05:07 emotilla