Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Tailwind plugin and docs #337

Merged
merged 27 commits into from
Aug 10, 2023
Merged

Add a Tailwind plugin and docs #337

merged 27 commits into from
Aug 10, 2023

Conversation

delucis
Copy link
Member

@delucis delucis commented Jul 14, 2023

What kind of changes does this PR include?

  • Changes to Starlight code

Description

  • Closes Support Tailwind CSS integration and styles #88
  • Adds a Tailwind plugin, available by installing @astrojs/starlight-tailwind, which will configure Tailwind for compatibility with Starlight
    • Disables Preflight (but re-enables its important border styling)
    • Configures Tailwind’s dark mode for compatibility with Starlight’s data-theme attribute
    • Links Starlight’s color custom properties to gray and accent in Tailwind’s theme.colors config.
    • Links Starlight’s font custom properties to sans and mono in Tailwind’s theme.fontFamily config.
  • Adds a Tailwind starter project at examples/tailwind/

Big shout out to @marcjulian for many of the tips in #88 that helped shape the work here. Very curious to hear feedback from folks about whether this solution works or not.

To do

Try it

Keeping this a draft while we wait for withastro/astro#7563 to be fixed, but you can try the starter template by cloning this branch, installing dependencies, then running cd examples/tailwind && pnpm dev.

@changeset-bot
Copy link

changeset-bot bot commented Jul 14, 2023

🦋 Changeset detected

Latest commit: adf7903

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight-tailwind Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Jul 14, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit adf7903
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/64d4a34b03d58e00082127cb
😎 Deploy Preview https://deploy-preview-337--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Jul 14, 2023
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Jul 14, 2023

size-limit report 📦

Path Size
/index.html 9.36 KB (0%)
/_astro/*.js 15.85 KB (0%)
/_astro/*.css 8.36 KB (0%)

@delucis delucis added the 🌟 minor Change that triggers a minor release label Jul 14, 2023
@github-actions github-actions bot removed the 🌟 core Changes to Starlight’s main package label Aug 4, 2023
@github-actions github-actions bot added the 🚨 action Changes to GitHub Action workflows label Aug 4, 2023
@github-actions github-actions bot added the 📚 docs Documentation website changes label Aug 4, 2023
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs LGTM! So excited for this feature!

Copy link
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs and example Markdown files all look great to me! Only added a small, non-blocking comment 🙌

@delucis delucis changed the title Expose a Tailwind plugin from the main Starlight package Add a Tailwind plugin and docs Aug 5, 2023
@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Aug 7, 2023
@delucis delucis marked this pull request as ready for review August 7, 2023 22:14
@HiDeoo
Copy link
Member

HiDeoo commented Aug 8, 2023

Super neat and clean ✨

I assume the withastro/starlight/examples/tailwind template would get aliased at some point in create-astro like Starlight?

Small question: any specific reason to use a .cjs file for the Tailwind configuration file considering they're supporting ESM now? Could be 1 less file type maybe for the end user?

@delucis
Copy link
Member Author

delucis commented Aug 8, 2023

any specific reason to use a .cjs file for the Tailwind configuration file

Mainly that it's what astro add tailwind currently generates. Tailwind docs also show CJS syntax like importing with require and exporting with module.exports (although usually with a .js extension).

I actually tried and all extensions work here (also .ts) and, perhaps most weirdly, you can use import or require in all of them thanks to how Tailwind processes the file during loading. But I thought it might be easiest to stick to what Astro does to have a better chance of matching what a user has.

I assume the withastro/starlight/examples/tailwind template would get aliased at some point

Ah yes, I'd been meaning to do that. Currently create astro has a hardcoded alias that maps starlight to the basics template. Would be nice to support anything after a slash to shorten things, e.g. --template starlight/tailwind.

Update: Proposed support for this here: withastro/astro#7993

@HiDeoo
Copy link
Member

HiDeoo commented Aug 8, 2023

But I thought it might be easiest to stick to what Astro does to have a better chance of matching what a user has.

Makes total sense, thanks.

Would be nice to support anything after a slash to shorten things

Ah yeah, that would be great indeed.

@marcjulian
Copy link
Contributor

Hey @delucis this looks awesome. Providing a starlightPlugin for preparing Tailwind to work well with Starlight styles is is perfect. It's event easy to swap out accent and gray color by just changing it in the tailwind config.

Would be nice to support anything after a slash to shorten things, e.g. --template starlight/tailwind.

Even shorter would be starwind ⭐ 🌬️

@marcjulian
Copy link
Contributor

I noticed two things while playing around with a custom theme.

  1. Changing accent to colors.red in tailwind.config.cjs updates the accent while in dev mode. However, the accent is overridden in the build output by the default Starlight colors.

Dev mode with accent: colors.red
CleanShot 2023-08-08 at 12 00 39

Build output with accent: colors.red
CleanShot 2023-08-08 at 12 01 04

I checked the generated CSS content. It does contain --sl-color-accent: #dc2626; (theme(colors.red.600) at the beginning of the CSS file in :root. But after all tailwind styles there is a block :root,::backdrop which redefines --sl-color-accent: hsl(var(--sl-hue-accent), 100%, 60%);, the default Starlight theme (coming from packages/starlight/style/props.css).

A workaround would be to add the customCss file back with the tailwind directives (8e3602b). This works because the tailwind styles are added last to the generated CSS file.

  1. When the starlightPlugin is enabled, I cannot override the theme styles with providing a customCss file.
    For example, if the theme should have a darker toolbar/sidebar and a lighter content area in dark mode or when I need to apply my own theme:
/* needs base directive otherwise vite throws: `@layer base` is used but no matching `@tailwind base` directive is present. */
@tailwind base;

@layer base {
  /* Dark Theme */
  :root {
    --sl-color-white: theme('colors.white');
    --sl-color-gray-1: theme('colors.gray.200');
    --sl-color-gray-2: theme('colors.gray.300');
    --sl-color-gray-3: theme('colors.gray.500');
    --sl-color-gray-4: theme('colors.gray.600');
    --sl-color-gray-5: theme('colors.gray.700');
    --sl-color-gray-6: theme('colors.gray.950'); /* darker toolbar/sidebar */
    --sl-color-black: theme('colors.gray.800'); /* brighter content */
  }
}

The custom theme styles are overridden by the styles provided by the starlightPlugin, but only in dev mode again.
CleanShot 2023-08-08 at 12 22 36

In build output, the custom styles take effect over the starlightPlugin styles.

CleanShot 2023-08-08 at 12 31 07

Perhaps allowing starlightPlugin to receive an option to disable the default style mapping from starlight would help here. This also eliminates having duplicate definitions of CSS variables and thus leading to a smaller CSS file.

@delucis
Copy link
Member Author

delucis commented Aug 8, 2023

Thanks for testing @marcjulian! Which Astro version was this with?

@marcjulian
Copy link
Contributor

[email protected] I forked this project and test it with the tailwind example.

@delucis
Copy link
Member Author

delucis commented Aug 8, 2023

Thanks for the testing and feedback @marcjulian! I’ve moved the docs and starter template back to the customCSS approach as this seems to be the best way to resolve all these issues.

@marcjulian
Copy link
Contributor

marcjulian commented Aug 8, 2023

Yes using customCss option solves both issues for dev mode and build. Thanks for the quick update.

@github-actions github-actions bot removed the 🌟 core Changes to Starlight’s main package label Aug 10, 2023
@delucis delucis merged commit 555826d into main Aug 10, 2023
@delucis delucis deleted the dx-387/tailwind-compat branch August 10, 2023 09:15
@astrobot-houston astrobot-houston mentioned this pull request Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 action Changes to GitHub Action workflows 📚 docs Documentation website changes 🌟 minor Change that triggers a minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Tailwind CSS integration and styles
6 participants