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(clerk-js): Add support for loading UI styles as first CSS stylesheet #4291

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 0 additions & 4 deletions packages/clerk-js/global.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/clerk-js/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@clerk/ui/styles.css' {
const content: string;
export default content;
}
16 changes: 13 additions & 3 deletions packages/clerk-js/src/ui/new/renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// TODO: don't import here
import '@clerk/ui/styles.css';

import { ClerkInstanceContext, OptionsContext } from '@clerk/shared/react';
import type { ClerkHostRouter } from '@clerk/shared/router';
import { ClerkHostRouterContext } from '@clerk/shared/router';
import type { ClerkOptions, LoadedClerk } from '@clerk/types';
import stylesheetURL from '@clerk/ui/styles.css';
import type { ElementType, ReactNode } from 'react';
import { createElement, lazy } from 'react';
import { createPortal } from 'react-dom';
Expand Down Expand Up @@ -43,6 +41,18 @@ export function init({ wrapper }: { wrapper: ElementType }) {
rootElement = document.createElement('div');
rootElement.setAttribute('id', 'clerk-components');
document.body.appendChild(rootElement);

// Just for completeness, we check to see if we've already added the stylesheet to the DOM.
const STYLESHEET_SIGIL = 'data-clerk-styles';
const existingStylesheet = document.querySelector(`link[${STYLESHEET_SIGIL}]`);
if (!existingStylesheet) {
const stylesheet = document.createElement('link');
stylesheet.href = stylesheetURL;
stylesheet.rel = 'stylesheet';
stylesheet.setAttribute(STYLESHEET_SIGIL, '');
// Add as first stylesheet so that application styles take precedence over our styles.
document.head.prepend(stylesheet);
}
}

const root = createRoot(rootElement);
Expand Down
16 changes: 11 additions & 5 deletions packages/clerk-js/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ const common = ({ mode }) => {
},
},
},
experiments: {
css: true,
},
};
};

Expand Down Expand Up @@ -173,12 +170,21 @@ const typescriptLoaderDev = () => {
};
};

/** @type { () => (import('webpack').RuleSetRule) } */
const clerkUICSSLoader = () => {
// This emits a module exporting the URL to the styles.css file.
return {
test: /packages\/ui\/dist\/styles\.css/,
type: 'asset/resource',
};
};

/** @type { () => (import('webpack').Configuration) } */
const commonForProd = () => {
return {
devtool: undefined,
module: {
rules: [svgLoader(), typescriptLoaderProd()],
rules: [svgLoader(), typescriptLoaderProd(), clerkUICSSLoader()],
},
output: {
path: path.resolve(__dirname, 'dist'),
Expand Down Expand Up @@ -297,7 +303,7 @@ const devConfig = ({ mode, env }) => {
const commonForDev = () => {
return {
module: {
rules: [svgLoader(), typescriptLoaderDev()],
rules: [svgLoader(), typescriptLoaderDev(), clerkUICSSLoader()],
},
plugins: [
new ReactRefreshWebpackPlugin({ overlay: { sockHost: devUrl.host } }),
Expand Down
Loading