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

feat: support browser runtime for tw plugin #1072

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions packages/uploadthing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,25 @@
}
},
"./tw": {
"import": {
"types": "./tw/index.d.ts",
"default": "./tw/index.js"
"browser": {
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
"import": {
"types": "./tw/index.d.ts",
"default": "./tw/index.browser.js"
},
"require": {
"types": "./tw/index.d.cts",
"default": "./tw/index.browser.cjs"
}
},
"require": {
"types": "./tw/index.d.cts",
"default": "./tw/index.cjs"
"default": {
"import": {
"types": "./tw/index.d.ts",
"default": "./tw/index.node.js"
},
"require": {
"types": "./tw/index.d.cts",
"default": "./tw/index.node.cjs"
}
}
},
"./fastify": {
Expand Down
79 changes: 0 additions & 79 deletions packages/uploadthing/src/tw.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/uploadthing/src/tw/index.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Config } from "tailwindcss";

import { uploadthingPlugin } from "./plugin";

export { uploadthingPlugin };

export function withUt(twConfig: Config) {
if (!twConfig.plugins) {
twConfig.plugins = [];
}

twConfig.plugins.push(uploadthingPlugin);
}
60 changes: 60 additions & 0 deletions packages/uploadthing/src/tw/index.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { dirname, sep } from "node:path";
import type { Config } from "tailwindcss";

import { uploadthingPlugin } from "./plugin";

export { uploadthingPlugin };

/**
* Add more here when additional UI packages are added
*/
const PACKAGES = ["react", "solid", "svelte", "vue"];

/**
* HOF for Tailwind config that adds the
* {@link uploadthingPlugin} to the Tailwind config
* as well as adds content paths to detect the necessary
* classnames
*/
export function withUt(twConfig: Config) {
const contentPaths = PACKAGES.map((pkg) => {
try {
const resolved = require.resolve(`@uploadthing/${pkg}`, {
paths: [...module.paths, process.cwd()],
});

return dirname(resolved) + sep + "**";
} catch {
return null;
}
}).filter((s) => s != null);

if (contentPaths.length === 0) {
// eslint-disable-next-line no-console
console.warn(`
[uploadthing]: Unable to resolve path for uploadthing UI packages. As a workaround, you can manually add the paths to your content paths:
- Find where your package manager has installed the distribution files, e.g. './node_modules/@uploadthing/react'.
Note: If you have a monorepo, you may need to look up the tree to find the correct path.
- Add the path to the 'content' field in your Tailwind configuration:
content: [
// your other content paths
'./node_modules/@uploadthing/react/dist**' // <-- add this line
]
`);
}

if (Array.isArray(twConfig.content)) {
twConfig.content.push(...contentPaths);
} else {
// content can be an object too with `files` property
twConfig.content.files.push(...contentPaths);
}

if (!twConfig.plugins) {
twConfig.plugins = [];
}

twConfig.plugins.push(uploadthingPlugin);

return twConfig;
}
23 changes: 23 additions & 0 deletions packages/uploadthing/src/tw/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import plugin from "tailwindcss/plugin";

/**
* UploadThing Tailwind plugin which injects custom variants
* for the built-in UI components
* @see https://docs.uploadthing.com/concepts/theming#theming-with-tailwind-css
*
* When using this, you need to specify `content` manually. For automatic
* detection, see {@link withUt}.
*/
export const uploadthingPlugin = plugin(({ addVariant }) => {
// Variants to select specific underlying element
addVariant("ut-button", '&>*[data-ut-element="button"]');
addVariant("ut-allowed-content", '&>*[data-ut-element="allowed-content"]');
addVariant("ut-label", '&>*[data-ut-element="label"]');
addVariant("ut-upload-icon", '&>*[data-ut-element="upload-icon"]');
addVariant("ut-clear-btn", '&>*[data-ut-element="clear-btn"]');

// Variants to select specific state
addVariant("ut-readying", '&[data-state="readying"]');
addVariant("ut-ready", '&[data-state="ready"]');
addVariant("ut-uploading", '&[data-state="uploading"]');
});
Loading