Using headless-ui with Fresh #606
-
Hi, I'm trying to use the Headless UI
Since this is a React library, I tried using the Preact compat layer, like so: import { Tabs } from "https://esm.sh/@headlessui/react@1.6.6?alias=react:preact/compat"; This got rid of the Typescript errors I had, but I get runtime errors:
Then I check the Preact documentation again and added all of the rest of the aliases: import { Tab } from "https://esm.sh/@headlessui/react@1.6.6?alias=react:preact/compat,react-dom/test-utils:preact/test-utils,react-dom:preact-compat,react/jsx-runtime:preact/jsx-runtime"; But that brings back the Typescript errors as well as the runtime error (slightly different paths due to different aliases). So I'm at a loss here - can I even get this to work? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 6 replies
-
Importing Ideally |
Beta Was this translation helpful? Give feedback.
-
instead of complex {
"imports": {
"react": "http://localhost:8080/preact@10.7.2/compat",
"preact": "http://localhost:8080/preact@10.7.2",
"preact/": "http://localhost:8080/preact@10.7.2/",
"preact-render-to-string": "http://localhost:8080/*preact-render-to-string@5.2.0",
"swr": "http://localhost:8080/*swr@1.3.0"
}
} |
Beta Was this translation helpful? Give feedback.
-
@ClaudiuCeia did you get headless-ui working well, I seem to be having issues with trapping event/state on headless ui controls, for example when toggling the state of a Switch, the switch state seems to break after first assignment. (using doc example from headless-ui site). |
Beta Was this translation helpful? Give feedback.
-
Thanks @lucacasonato - your suggestion works for me too. I'm not sure what the "@headlessui/react": "https://esm.sh/@headlessui/react@1.6.6?alias=react:preact/compat,react-dom:preact/compat,@types/react:preact/compat&deps=preact@10.10.3" Slightly offtopic - I'd love it if there was a way for me to pin some dependency down to a version and have that reused all over (ie: @scott-ling I tried it, seems to work fine for me with: const [checked, setChecked] = useState(false);
const toggleChecked = () => setChecked(!checked);
// ...
<Switch
checked={checked}
onChange={toggleChecked}
class={tw`${
checked ? "bg-blue-600" : "bg-gray-200"
} relative inline-flex h-6 w-11 items-center rounded-full`}
>
<span class={tw`sr-only`}>Enable notifications</span>
<span
class={tw`${
checked ? "translate-x-6" : "translate-x-1"
} inline-block h-4 w-4 transform rounded-full bg-white`}
/>
</Switch> |
Beta Was this translation helpful? Give feedback.
-
This doesn't seem to work with all of For example, if you take the example for the menu component and put it into an island: https://headlessui.com/react/menu It will fail when you click it with:
This seems to be happening in Lines 4 to 25 in fd4e2bb I figured out that if you wrap This seems to be because fresh doesn't like rendering empty fragments in islands. See also this issue: #502 |
Beta Was this translation helpful? Give feedback.
-
For any one that still having issues wtih some of the components, this import worked for me
Got it from here #432 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Importing
https://esm.sh/@headlessui/react@1.6.6?alias=react:preact/compat,react-dom:preact/compat,@types/react:preact/compat&deps=preact@10.10.0
works for me.Ideally
https://esm.sh/@headlessui/react@1.6.6?alias=react:preact/compat,react-dom:preact/compat,@types/react:preact/compat&external=preact
would work, but that seems to be running into some esm.sh bug. cc @ije: theexternal=preact
is not applying to most imports it seems.