I've been working with Material UI for quite a bit and I've liked it quite a lot.
I've also liked Deno and its framework Fresh. The main downside for Fresh is that I have not found any documentation to integrate any graphic component library with it.
Is it possible? Does anyone have an example?
Confirmation - it is possible to use Material UI components in a Deno Fresh app.
Took the past several hours to get working but ultimately it comes down setting up your
deno.json
imports correctly (I have it aliased as$mui
in mydeno.json
file pasted below).The tricky parts were
Let's start by breaking down the import needed that I've defined in my
deno.json
imports array.The import URL provided for my
$mui
alias is a way to use the Material-UI (MUI) library with Preact in a project, specifically when importing from the esm.sh CDN.Let's break down the import to understand what each part does:
Base URL and Package Name:
https://esm.sh/@mui/[email protected]
@mui/material
) and its version (5.8.7
). It specifies that you're importing the Material-UI library, version 5.8.7.Aliases (
alias
Query Parameter):?alias=react:preact/compat,react/jsx-runtime:preact/compat/jsx-runtime
alias
) any imports ofreact
andreact/jsx-runtime
within the MUI package withpreact/compat
andpreact/compat/jsx-runtime
, respectively.react:preact/compat
means "wherever MUI is importing React, use Preact Compat instead."react/jsx-runtime:preact/compat/jsx-runtime
means "wherever MUI is importing React's JSX runtime, use Preact's Compat JSX runtime instead."Dependencies (
deps
Query Parameter):&[email protected]?dts
10.8.1
.?dts
at the end indicates that TypeScript definitions are also requested.Putting it all together, this URL is used to import the Material-UI library from the esm.sh CDN, while ensuring that it works with Preact instead of React. It does this by aliasing React to Preact Compat, making it possible to use MUI (which is originally built for React) in a Preact project. This is particularly useful in environments like Deno, where you might be working with Preact and want to leverage the Material-UI component library.
Here's my entire
deno.json
fileHere's my
routes/index.tsx
fileHere's my
routes/_app.tsx
fileAnd lastly, the second gotcha with integrating Material UI components in a deno fresh app - being aware that some Mui UI Components only work within Deno Fresh islands (components defined in the islands folder of your deno fresh project).
The Mui UI components that need to be created within islands are the components that require front-end reactivity to work properly.
As an example, I had to move the Mui UI AppBar into an island for it to work properly. Then you can import this island component. Once it's in an island, you can import this AppBar island into any other deno fresh page/component for use and it works as expected - it just requires some front-end reactivity so must be defined in an island in accordance to deno fresh's island architecture.
And last but not least, as proof of it properly working, here is an image of the deno fresh app with the AppBar Mui ui component being used. And lala, both Stylistically and functionally Mui UI Components work properly in a deno fresh project.
Hope that helps!