diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b451c68..84b2d3c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,28 +6,21 @@ on: pull_request: jobs: - css: + test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: - ruby: ['3.1', '3.4'] + ruby: ['3.3', '3.4'] steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - run: bundle exec rake css - - # Capybara integration test for the utility-nav server clock. Needs headless - # Chrome (preinstalled on ubuntu-latest) and Rails 8 (Ruby >= 3.2). - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: ruby/setup-ruby@v1 + - uses: actions/setup-node@v4 with: - ruby-version: '3.4' - bundler-cache: true + node-version: 22 + cache: npm + cache-dependency-path: test/dummy/package-lock.json - run: bundle exec rake test diff --git a/.gitignore b/.gitignore index e15aa1e..4bef1b3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ /src/ /test/dummy/tmp/ /test/dummy/log/ -/test/dummy/app/assets/builds/ /test/dummy/node_modules/ +/test/dummy/app/assets/builds/* +!/test/dummy/app/assets/builds/.keep +/tmp/ diff --git a/Gemfile b/Gemfile index 49e2c88..cc2f070 100644 --- a/Gemfile +++ b/Gemfile @@ -1,26 +1,33 @@ source 'https://rubygems.org' # Specify your gem's dependencies in active_admin_theme.gemspec + +# Run against an unreleased ActiveAdmin, such as a branch with the layout hooks: +# ACTIVEADMIN_PATH=../activeadmin bundle exec rake test +# ACTIVEADMIN_BRANCH=extensible-layout bundle exec rake test +if ENV["ACTIVEADMIN_PATH"] + gem "activeadmin", path: ENV["ACTIVEADMIN_PATH"] +elsif ENV["ACTIVEADMIN_BRANCH"] + gem "activeadmin", github: ENV.fetch("ACTIVEADMIN_REPO", "senid231/activeadmin"), branch: ENV["ACTIVEADMIN_BRANCH"] +end gemspec +# Dummy ActiveAdmin 4 app (test/dummy): importmap + propshaft, Tailwind CSS +# built with the npm CLI. It hosts the Capybara tests and the screenshot +# scripts. group :development, :test do gem 'rake' - gem 'sassc' -end - -# Dummy ActiveAdmin app (test/dummy) that hosts the Capybara integration test -# (`rake test`) and sources the neutral README screenshot. Sprockets + -# sassc-rails is the pipeline AA 3.x targets (it emits `//= require` directives -# and SCSS). Rails 8 needs Ruby >= 3.2, so keep these out of the 3.1 bundle — -# `rake css` still runs on 3.1 without them. -if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2') - group :development, :test do - gem 'rails', '~> 8.0' - gem 'sprockets-rails' - gem 'sassc-rails' - gem 'sqlite3' - gem 'puma' - gem 'capybara' - gem 'cuprite' - end + gem 'rails', '~> 8.0' + gem 'propshaft' + gem 'importmap-rails' + gem 'sqlite3' + gem 'puma' + gem 'capybara' + gem 'cuprite' + gem 'rspec-expectations' + # Drives the dummy app in the tests of active_admin_theme/page_objects. Not on + # RubyGems yet, so pinned to a commit. + gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', ref: '8ec1677d4118559af117421fbf6576cdd5e33721' + # Tests the searchable select with its None filter option. Pinned until the option is released. + gem 'active_admin_filter_extensions', github: 'activeadmin-plugins/active_admin_filter_extensions', ref: '31d4797a30a5e964f6eb0e15fe52f8e6097d7093' end diff --git a/README.md b/README.md index 67806d0..90460e9 100644 --- a/README.md +++ b/README.md @@ -1,486 +1,937 @@ # ActiveAdminTheme -Custom ActiveAdmin templates +A dense desktop theme and a set of shared components for [ActiveAdmin](https://github.com/activeadmin/activeadmin) 4. +ActiveAdmin 4 keeps its menu in a left sidebar and gives breadcrumbs and titles a lot of room. That works on mobile but wastes width on a laptop with a large table and many filters. This theme keeps the compact desktop layout of theme 3 and keeps ActiveAdmin 4's mobile support: -## Installation -As active_skin is the css theme for the [activeadmin](https://github.com/activeadmin/activeadmin) administration framework - you have to install if first. +- **Desktop** (768px and wider): a horizontal top menu, compact breadcrumbs and title, filters left of the table (collapsible). +- **Mobile** (narrower than 768px): the menu opens in a drawer, filters sit above the table, forms and show pages use the full width. +- Index tables scroll sideways when they are wider than the screen, and cells stay on one line. +- Theme 3 colors, density, and typography on top of ActiveAdmin 4's own dark mode. + +Theme 4 needs ActiveAdmin 4 (`>= 4.0.0.beta23`) and Ruby 3.3 or newer. For ActiveAdmin 3, use theme 3.x (branch `3.x`). See [docs/migrating-from-v3.md](docs/migrating-from-v3.md). -#### As a Gem -Having active admin installed add the following line to your application's Gemfile: +## Installation +Add the gem and install it: ```ruby -gem 'active_admin_theme' +gem "active_admin_theme" ``` -And then execute: +The gem requires `active_admin` first, so its views and partials win over ActiveAdmin's own. Do not reorder that. - $ bundle +### CSS -Or install it yourself as: +ActiveAdmin 4 builds its CSS with Tailwind CSS 4. Your Tailwind entry file (generated by `rails g active_admin:assets`, usually `app/assets/stylesheets/active_admin.css`) must import the theme CSS after Tailwind. Pick one route. - $ gem install active_admin_theme +**From the installed gem.** The gem's location differs between machines (a laptop, a Docker image, CI), so the path must not be committed. The task writes it into a generated file next to your entry file, and the entry file only imports that file by a fixed name: -#### As a NPM module (Yarn package) -Execute: +```sh +bin/rails active_admin_theme:css +``` - $ npm i @activeadmin-plugins/active_admin_theme +The entry file gets this block, which is the same on every machine, so commit it: -Or +```css +@import "tailwindcss"; +@config "../../../tailwind-active_admin.config.js"; - $ yarn add @activeadmin-plugins/active_admin_theme +/* active_admin_theme:begin */ +@import "./active_admin_theme.generated.css"; +/* active_admin_theme:end */ +``` -Or add manually to `package.json`: +The task writes `active_admin_theme.generated.css`, which holds the machine-specific path. Add it to `.gitignore`: ``` -"dependencies": { - "@activeadmin-plugins/active_admin_theme": "^3.1.1" -} +/app/assets/stylesheets/active_admin_theme.generated.css ``` -and execute: - $ yarn +Run the task before every Tailwind build, because the generated file does not exist on a fresh checkout and its path changes when the gem upgrades. For example, in the build script that runs Tailwind: -## Usage +```sh +bin/rails active_admin_theme:css && npx tailwindcss -i app/assets/stylesheets/active_admin.css -o app/assets/builds/active_admin.css +``` -In your base stylesheet entry point `active_admin.scss` (as example), add line: +If that is awkward for your build, use the npm route. + +**From the npm package.** This avoids a machine-specific path and a build step, and fits hosts that already install `@activeadmin/activeadmin` from npm: + +```sh +npm install @activeadmin-plugins/active_admin_theme +``` -#### As a Gem via Sprockets ```css -@import 'wigu/active_admin_theme'; +@import "tailwindcss"; +@config "../../../tailwind-active_admin.config.js"; +@import "@activeadmin-plugins/active_admin_theme/css"; +``` + +Keep the npm package and the gem on the same version. + +Tailwind only generates the classes it finds in files it scans. Tailwind 4 reads `@source` lines in CSS. The theme prints them, so you do not call `bundle show` yourself: + +```sh +bin/rails active_admin_theme:tailwind:sources +``` + +To write them to a file, name it in `ACTIVE_ADMIN_THEME_SOURCES`. The paths in the file are relative to the file, but they still depend on where the gem is installed, so git-ignore it as you do the generated CSS import, and run the task in the same build step: + +```sh +ACTIVE_ADMIN_THEME_SOURCES=app/assets/stylesheets/active_admin_theme.sources.css bin/rails active_admin_theme:tailwind:sources +``` + +```css +@import "./active_admin_theme.sources.css"; +``` + +Don't commit the printed output, because its paths are absolute. With a Tailwind config file, `ActiveAdminTheme.tailwind_content` returns the globs for `content`, so no `execSync` is needed. + +### JavaScript + +ActiveAdmin 4 renders its own import map, `ActiveAdmin.importmap`, not the import map in your `config/importmap.rb`. A `pin` in your own `config/importmap.rb` loads nothing on ActiveAdmin pages. + +The theme's engine pins its modules into `ActiveAdmin.importmap`, so importmap hosts need no setup. To load one of your own scripts on ActiveAdmin pages, pin it into the same map, for example in `config/initializers/active_admin.rb`: + +```ruby +ActiveAdmin.importmap.pin "my_admin_script", to: "my_admin_script.js" +``` + +ActiveAdmin pages import the modules listed in `ActiveAdminTheme.javascripts`. With an ActiveAdmin that has the `javascript_modules` setting ([activeadmin/activeadmin#9148](https://github.com/activeadmin/activeadmin/pull/9148)), the theme adds them to that setting after your initializers run. With an older ActiveAdmin, the theme's own `_html_head` imports them. The default list is: + +```ruby +%w[ + active_admin_theme/layout + active_admin_theme/server_clock + active_admin_theme/tabs + active_admin_theme/dropdown_menu + active_admin_theme/table_crisscross + active_admin_theme/searchable_select + active_admin_theme/confirm_dialog + active_admin_theme/panel_toggle +] +``` + +With Sprockets, the engine adds the theme scripts and the vendored Tom Select to `config.assets.precompile`, so don't list them yourself. Propshaft needs nothing. + +Change the list in an initializer to load fewer scripts or to add the optional ones: + +```ruby +ActiveAdminTheme.javascripts -= %w[active_admin_theme/table_crisscross] +ActiveAdminTheme.javascripts += %w[active_admin_theme/secret_reveal] ``` -#### As a NPM module (Yarn package) via Webpacker or any other assets bundler +**Your own watched directory.** With an ActiveAdmin that has no `importmap_watches` setting ([activeadmin/activeadmin#9153](https://github.com/activeadmin/activeadmin/pull/9153)) and with `config.importmap.sweep_cache` on, each `ActiveAdmin.importmap.cache_sweeper(watches:)` call replaces the previous watcher. The theme makes that call from an `after_initialize` block that its initializer registers, so it runs after any `config.after_initialize` written in an engine or application class, and replaces that call's watcher. To watch your own directory too, register your block from an initializer that runs after the theme's, and pass every directory: + +```ruby +initializer "my_admin.importmap", after: "active_admin_theme.importmap" do |app| + app.config.after_initialize do + ActiveAdmin.importmap.cache_sweeper(watches: [ + ActiveAdmin::Engine.root.join("app/javascript"), + ActiveAdminTheme::Engine.root.join("app/javascript"), + root.join("app/javascript") + ].map(&:to_s)) + end +end +``` + +**jsbundling hosts.** The npm package exports each module (`@activeadmin-plugins/active_admin_theme/layout`, `.../server_clock`, and so on). Import the ones you want from your `active_admin` entry file, and override `app/views/active_admin/_html_head.html.erb` to load your bundle. + +## Overriding + +A host partial with the same path overrides the theme's. Copy the partial from the gem into your app's `app/views/active_admin/` and edit it. The theme's partials are: + +- `_header_leading`: the menu button and the site title. +- `_header_navigation`: the top menu. +- `_main_navigation`: the mobile drawer menu. +- `_header_actions`: the right end of the header. In order: the dark mode switch, the server clock, `_header_links`, the current user, and the logout icon. +- `_header_links`: empty. Override it to add your own links between the clock and the current user. +- `_page_heading`, `_breadcrumbs`, `_page_title`: the breadcrumbs and the page title, on one row of the dark title band. A host copy must keep light text there: use `--aat-titlebar-text` and `--aat-titlebar-muted`. +- `_page_content`: the grid that holds the sidebar and the page, and the tab that collapses the sidebar. +- `_sidebar`. +- `_flash_messages`: the flash bands under the title band. +- `kaminari/*`: the page links under an index table, with "First", "Next ›" and "Last »". + +The index footer is one line under the table: per page, page links, the count, and the download links. Its English wording ("Displaying Orders 1 - 30 of 60 in total") comes from the theme's `config/locales/active_admin_theme.en.yml`; your own locale file overrides it, and other languages keep ActiveAdmin's text. + +ActiveAdmin renders these partials from its own layout once it has the layout hooks ([activeadmin/activeadmin#9149](https://github.com/activeadmin/activeadmin/pull/9149) to [#9152](https://github.com/activeadmin/activeadmin/pull/9152)). Until then, the theme also ships copies of the layout, `_site_header`, `_page_header`, and `_html_head` in `app/views_compat` that render the same partials. The engine adds that directory to the view paths only when the installed ActiveAdmin lacks the hooks, so override the small partials above rather than those copies. + +A host Arbre component with the same builder method name replaces the theme's, as long as it loads after the theme's. Components in `app/admin_components` (or any file loaded in a `to_prepare` block) do. + +## Colors + +All colors are CSS custom properties with a light value on `:root` and a dark value under `.dark`. Override any of them in your own CSS: ```css -@import '@activeadmin-plugins/active_admin_theme'; +:root { + --aat-header-bg: #1b3a57; + --aat-button: #1f6f4a; +} +.dark { + --aat-surface: #20242b; +} +``` + +| Property | Used for | +| --- | --- | +| `--aat-page-bg`, `--aat-surface`, `--aat-surface-2`, `--aat-surface-hover`, `--aat-selected-row` | Page; panels, attributes tables and the tab content box; table headers, hover, selected rows | +| `--aat-text`, `--aat-text-muted`, `--aat-border` | Text and borders | +| `--aat-text-success`, `--aat-text-warning`, `--aat-text-danger` | Success, warning and danger text, such as a confirmation, a warning label or an error message. Readable (4.5:1 or better) on the page and panel surfaces in both modes. Use as `text-(--aat-text-danger)`. The `--aat-status-*` and `--aat-flash-*` colours are fills and are too light as text on the dark surface. | +| `--aat-input-bg`, `--aat-input-border` | Form controls | +| `--aat-accent`, `--aat-link`, `--aat-delete-link` | Accent, links, and delete links | +| `--aat-button`, `--aat-button-hover`, `--aat-button-text` | Buttons and action items. Every action item is a filled button. | +| `--aat-header-bg`, `--aat-header-text`, `--aat-header-border`, `--aat-menu-*` | Header and top menu | +| `--aat-titlebar-bg`, `--aat-titlebar-text`, `--aat-titlebar-muted` | Title band: background, page title, breadcrumbs. Dark in both modes. | +| `--aat-table-header`, `--aat-filter-label` | Index table header; filter and form field labels | +| `--aat-flash-notice`, `--aat-flash-alert`, `--aat-flash-warning`, `--aat-flash-error`, each with a `-border` variant | Flash band fill and bottom border, the same in both modes. Warning defaults to the alert colours, as in v3. Any other type is gray (`--aat-flash-other`); give it a colour with `.aat-flash[data-flash="info"] { --aat-flash-color: ...; --aat-flash-border: ...; }`. | + +Two more properties set sizes: + +- `--aat-font-size` (default `0.75rem`, 12px) is the base text size of the page, tables, forms, filters, and buttons. Smaller texts, such as filter labels and status tags, scale from it. +- `--aat-label-cap` is the widest the label column of a show page's `attributes_table` gets. The column takes the width of its labels up to the cap; a longer label wraps. + +## DSL + +### `columns` + +ActiveAdmin 4 removed `columns` and `column`. The theme restores them, with separate control for mobile and desktop. + +```ruby +columns "1:3" do # desktop: 1/4 + 3/4; mobile: stacked + column { ... } + column { ... } +end + +columns "1:1", mobile: "1:1" do # side by side on mobile too + ... +end + +columns do # no ratio: equal widths, any number of columns + ... +end +``` + +- The ratio string lists one weight per column. Pixel widths are not supported. +- On mobile, columns stack unless `mobile:` sets a ratio. +- A column has a minimum width of `--aat-col-min` (14rem). When the row has no room for it, the column moves to the next line instead of clipping its content. +- `column span: N` from ActiveAdmin 3 still works and counts as the column's weight. + +### `tabs` + +```ruby +tabs do + tab("Details") { ... } + tab("History", id: "history") { ... } +end +``` + +Tabs use Flowbite's tabs, which ActiveAdmin already loads. A tab's panel id is its parameterized title unless `id:` sets it. A title already used on the page gets `-2`, `-3` and so on, and an id that does not start with a letter gets a `tab-` prefix. + +A URL hash opens a tab, on page load and when the hash changes. `#history` opens the tab whose panel id is `history`. A hash that names an element inside a hidden tab opens that tab, and every tab around it, and scrolls to the element. Clicking a tab writes its panel id to the hash without adding a history entry. The `active_admin_theme/tabs` script does this. It is in the default list. + +### `panel` + +ActiveAdmin 4 still has `panel`, but no longer wraps sidebar content or the show page's `attributes_table` in one. The theme wraps both again: sidebar sections in a panel titled with the section name, and `attributes_table` on a show page in a "Model Details" panel. `Panel#header_action` is not restored. + +A panel can collapse. `collapsible: true` turns the title into a button (`aria-expanded`, `aria-controls`) that shows and hides the body, and `collapsed: true` starts it closed. The body is hidden in the HTML, so the page does not flash it. Enter and Space work because the title is a real button: + +```ruby +panel "JSON", collapsible: true, collapsed: true do + pre record.payload +end ``` -## Customising +It works for a panel inside a sidebar block too. The default script `active_admin_theme/panel_toggle` does the toggling and sends a bubbling `aat:panel:toggle` event with `detail: {panel, expanded}`. An app that removes it from `ActiveAdminTheme.javascripts` leaves a collapsed panel closed for good. A panel without `collapsible:` is unchanged. + +## Components + +Every component but the select widget is opt-in: nothing changes for an app that does not call it or load its script. In a `content do` block, wrap the helpers that return strings (`with_tooltip`, `pre_wrap`) in `text_node`. -Set any of the variables below *above* the import line: +### `status_tag` -```scss -$skinMainFirstColor: #A5A7AA; -$skinMainSecondColor: #0066CC; -$skinBorderWindowColor: #B8BABE; +Tags have a solid fill and white text, as in theme 3, the same in light and dark mode. `ok` (also `published`, `complete`, `completed`, `green`) is green, `yes` and `info` (also `notice`, `blue`) are blue, `warning` (also `warn`, `orange`) is amber, `error` (also `errored`, `red`, `danger`) is red, and anything else, such as `no`, is gray. `empty` shows only muted text. For any other value, define a CSS custom property named `--aat-status-` in your CSS. For a one-off color, pass `color:` (a hex, named, `rgb()`, or `hsl()` color; anything else raises `ArgumentError`): -@import 'wigu/active_admin_theme'; +```ruby +status_tag "Shipped" # uses --aat-status-shipped, if you define it +status_tag "Late", color: "#b91c1c" ``` -Variables are typed. A value of the wrong kind — `none` where a colour is -expected, or a length without its unit — fails the build with a message -naming the variable, instead of silently emitting CSS the browser discards. +Tags are square, small, and shown in uppercase, as in theme 3. The case is CSS only, so the HTML keeps the text you pass, while a browser test driver reads it in uppercase. The `StatusTag` page object and the base gem's `have_cell` and `have_row` compare String text ignoring case. + +### `notice` + +An inline note: a tinted box with a colored edge, readable in light and dark mode. `kind:` is `info` (the default), `success`, `warning` or `danger`; an unknown kind raises `ArgumentError`. A warning or danger has `role="alert"`, the others `role="status"`. A block builds the body. + +```ruby +notice "The export may take a minute." +notice "Check the dates before you export", kind: :warning +notice(kind: :danger) { para "Payment failed" } +``` + +### `truncate_text` + +One line of text with an ellipsis and the full value in the `title` attribute: + +```ruby +truncate_text(order.note, length: 40, max_width: "12rem") +``` + +With `popup: true` the full value shows in a popup on hover and on keyboard focus, in place of the `title`. The element is focusable and names the popup in `aria-describedby`. The popup is CSS only, and a scrolling table does not clip it: + +```ruby +truncate_text(order.note, max_width: "12rem", popup: true) +``` + +### `with_tooltip` + +A plain `title` hint by default. With `html: true`, the tooltip is sanitized HTML shown by ActiveAdmin's Flowbite tooltip. The HTML tooltip is positioned inside its container, so a wrapper with `overflow` (a scrolling table) can clip it. + +```ruby +text_node with_tooltip("42", "Total orders") +text_node with_tooltip("42", "Total orders", html: true) +``` + +### `pre_wrap` + +Preformatted text that wraps long lines. Blank text renders nothing, so an `attributes_table` row shows ActiveAdmin's empty marker. `max_height:` takes a CSS length or a number of pixels, caps the height, and scrolls the rest. The capped block can take keyboard focus: + +```ruby +text_node pre_wrap(response.body) +text_node pre_wrap(response.body, max_height: "20rem") +row(:payload) { |order| pre_wrap(order.payload, max_height: 240) } +``` + +### `secret_reveal` + +A masked value with an eye icon before it. A click fetches and shows the value, and the icon turns to an eye-off icon that hides it again. The URL must return `{"value": "..."}`. Needs the `active_admin_theme/secret_reveal` script. -### Dark mode +```ruby +secret_reveal url: secret_admin_account_path(account), mask: "••••••••" +``` + +With `value:` instead of `url:`, the value is in the page, in a `