轨道 7 +导入地图/任何东西+ TailwindCSS + JS = 困惑

发布于 2025-01-09 20:29:59 字数 1774 浏览 4 评论 0 原文

首先,我更像是一个 Rails 后端人员。今天的 JS 世界让我感到害怕。我知道这是一个非常基本的问题,但我已经绞尽脑汁好几天了,试图这个 out。我不知道为什么我不能在 HTML 中的某个位置放置 CDN 链接并获取我需要的所有 JS。那些都是美好的时光……

无论如何,我有一个几乎全新的 Rails 7 应用程序,它使用导入映射(它们都是吗?),我试图从 https://tailwindui.com/preview (该页面的第一个导航栏)工作。它开始弹出打开,没有悬停效果,并且无法关闭。我的目标是使用更多这些组件,但我读过的所有文档似乎都让我觉得缺少一个我不理解的部分。

Gemfile 包含 gem "tailwindcss-rails", "~> 2.0" # github: "rails/tailwindcss-rails"

app/assets/stylesheets/application. tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

app/assets/javascript/application.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails

// what else needs to go here???

config/tailwind.config.js

// const defaultTheme = require('tailwindcss/defaultTheme')

// module.exports = {
//   content: [
//     './app/helpers/**/*.rb',
//     './app/javascript/**/*.js',
//     './app/views/**/*'
//   ],
//   theme: {
//     extend: {
//       fontFamily: {
//         sans: ['Inter var', ...defaultTheme.fontFamily.sans],
//       },
//     },
//   },
//   plugins: [
//     require('@tailwindcss/forms'),
//     require('@tailwindcss/aspect-ratio'),
//     require('@tailwindcss/typography'),
//   ]
// }

我还需要放置什么才能使其正常工作?谢谢你填补了我大脑中缺失的部分。

First, I'm much more of a Rails back end person. The JS world today scares me. I know this is a super basic question, but I've racked my brain for a solid couple days trying to figure, this out. I don't know why I can't put a CDN link somewhere in my HTML and get all the JS I need. Those were the good ol' days...

Anyway, I have a nearly fresh Rails 7 app that uses import-maps (do they all?) and I'm trying to get a dropdown "component" from https://tailwindui.com/preview (the first navbar from that page) working. It starts popped open, no hover effects, and is unable to close. My goal is to use more of those components, but all the docs I read seem to leave me thinking there's a missing piece I'm not understanding.

Gemfile contains gem "tailwindcss-rails", "~> 2.0" # github: "rails/tailwindcss-rails"

app/assets/stylesheets/application.tailwind.css:

@tailwind base;
@tailwind components;
@tailwind utilities;

app/assets/javascript/application.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails

// what else needs to go here???

config/tailwind.config.js

// const defaultTheme = require('tailwindcss/defaultTheme')

// module.exports = {
//   content: [
//     './app/helpers/**/*.rb',
//     './app/javascript/**/*.js',
//     './app/views/**/*'
//   ],
//   theme: {
//     extend: {
//       fontFamily: {
//         sans: ['Inter var', ...defaultTheme.fontFamily.sans],
//       },
//     },
//   },
//   plugins: [
//     require('@tailwindcss/forms'),
//     require('@tailwindcss/aspect-ratio'),
//     require('@tailwindcss/typography'),
//   ]
// }

What else do I need to put where to get this working? Thank you kindly for filling in the missing pieces in my brain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅淡淡 2025-01-16 20:29:59

我知道你问这个问题已经有一段时间了,但我最近一直在与这个问题作斗争,我想谈谈我的发现,以防有人想知道。

因此,Tailwind UI 提供了 3 种变体的组件:HTML、React 和 Vue。如果您选择 HTML,则必须弄清楚如何编写自己的 JavaScript。 这个答案提到了这一点,但我会详细说明这一点。

除了在他们的网站上提到“做你自己的 JavaScript”之外,他们还向我们提供了在添加 JavaScript 时应该如何设置组件样式的说明:

          <!--
            Dropdown menu, show/hide based on menu state.

            Entering: "transition ease-out duration-100"
              From: "transform opacity-0 scale-95"
              To: "transform opacity-100 scale-100"
            Leaving: "transition ease-in duration-75"
              From: "transform opacity-100 scale-100"
              To: "transform opacity-0 scale-95"
          -->

然而,这并没有真正告诉我们必须编写代码我们自己。当然,我们还缺少一些东西,对吧?事实证明,并非如此。

基本上,布局的“动态”方面是通过 Headless UI 实现的,这是一组开发和开发的组件。由同一个人维护。它只有三个公共包:@headlessui/vue、@headlessui/react 和@headlessui/tailwindcss。

我运行了 npm i @headlessui/tailwindcss 并将 @headlessui/tailwindcss 添加到插件中,但它并没有真正做太多事情。如果您检查该包的源代码,您会发现一个非常小的 index.js 它只添加了一些实用程序类。所以这没有帮助,我不得不刮掉它。

到目前为止,最可行的选择似乎是获取 React 或 Vue 版本的模板并将其集成到您的管道中。否则你将不得不编写一堆 JS 来基本上自己重新实现无头 UI。

我仍在寻找基于 importmap 的解决方案来集成 Vue,而 webpack 应该非常简单。

I know it's been a while since you've asked this question, yet I've been fighting this thing recently and I'd like to talk about what I've found, in case anybody's wondering.

So, Tailwind UI provides components in 3 variants: HTML, React, and Vue. If you pick HTML, you'll have to figure out how to write your own JavaScript. this answer mentions that, but I'll elaborate on that point, though.

Aside from mentioning "do your own JavaScript" on their website, they also give us instructions how exactly we should style our components when we add JavaScript:

          <!--
            Dropdown menu, show/hide based on menu state.

            Entering: "transition ease-out duration-100"
              From: "transform opacity-0 scale-95"
              To: "transform opacity-100 scale-100"
            Leaving: "transition ease-in duration-75"
              From: "transform opacity-100 scale-100"
              To: "transform opacity-0 scale-95"
          -->

Yet, that doesn't really tell us that we must code it ourselves. Surely, there's something we're missing, right? Turns out, not really.

Basically, the "dynamic" aspect of the layout is implemented via Headless UI, a set of components developed & maintained by the same people. It only has three public packages: @headlessui/vue, @headlessui/react and @headlessui/tailwindcss.

I ran npm i @headlessui/tailwindcss and added @headlessui/tailwindcss to the plugins, but it didn't really do much. If you inspect the source code for the package, you'll find a pretty tiny index.js which only adds some utility classes. So that didn't help and I had to scratch it.

So far it seems the most viable option is to grab a React or Vue version of the template and integrate those into your pipeline. Otherwise you'll have to write a bunch of JS to basically reimplement headless UI yourself.

I'm still looking for an importmap-based solution to integrate Vue, while webpack should be pretty much straightfoward.

还给你自由 2025-01-16 20:29:59

他们的 HTML 代码中没有 JavaScript。在 TailwindUI 主页上,他们解释道:

React 和 Vue 的可访问、交互式示例,由
无头 UI,如果您愿意编写任何必要的 JS,还可添加普通 HTML
你自己。

所以我想在这种情况下你必须自己编写必要的JS。

There is no JavaScript in their HTML code. On TailwindUI homepage, they explain:

Accessible, interactive examples for React and Vue powered by
Headless UI, plus vanilla HTML if you’d rather write any necessary JS
yourself.

So I guess in this case you have to write the necessary JS yourself.

澜川若宁 2025-01-16 20:29:59

我同意你的观点,有一个缺失的部分。我在尝试在使用 创建的新 Rails 7.1 应用程序中安装 daisyUI tailwindcss 插件时一直在解决这个问题-css=tailwind。看起来,要使用这些 tailwind 插件(tailwindui 和 daisyUI),导入映射的默认 javascript 使用是不够的。我可能还遗漏了一些东西,但这些 UI 框架想要通过 JS 包管理器(node、yarn、bun)安装。一旦我切换到bun,我就可以更轻松地让插件工作。

需要:

  • 在本地安装 bun (如果不这样做,rails 会尝试回退到yarn)
  • 运行 rails new myapp --javascript=bun --css=tailwind

这个答案还提供了更多详细信息,其中包含密钥详细信息是:

要使用任何其他 tailwind 插件,您必须运行完整的 node.js 版本的 tailwind

似乎缺少的部分是 --css=tailwind 所依赖的 >standalone tailwind cli(由 importmaps 处理 JavaScript)和 tailwindcss-rails gem 不再是可行的解决方案作为当你想使用 tailwind 插件时。

也许这已经改变了,或者至少为我们指明了研究如何使用 tailwind cli 解决插件问题的方向,因为它实际上并不是 Rails 的核心问题。

I agree with you, there is a missing piece. I've been working through this while attempting to install the daisyUI tailwindcss plugin in a new Rails 7.1 app created with -css=tailwind. It's looking like, to use these tailwind plugins (tailwindui and daisyUI), the default javascript use of import-maps falls short. I may be missing something as well, but these UI frameworks want to install through a JS package manager (node, yarn, bun). Once I switched over to bun, I've had a much easier time getting the plugin working.

That required:

  • installing bun locally (if you don't, rails tries to fallback to yarn)
  • run rails new myapp --javascript=bun --css=tailwind

This answer gives more details as well, with the key detail being:

To use any other tailwind plugins you must run a full node.js version of tailwind

Seems like the missing piece is that the standalone tailwind cli that is relied on by --css=tailwind (with javascript handled by importmaps) and the tailwindcss-rails gem are no longer viable solutions as soon as you want to use a tailwind plugin.

Maybe that's changed or least points us in the direction of investigating how to solve the plugin problem with the tailwind cli as it's not really a Rails issue at the core.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文