-
Notifications
You must be signed in to change notification settings - Fork 33
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
chore: add tailwind styles to lit migration #4804
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5832fae
tailwind styles support
fpbrault be918d6
fix react/angular build
fpbrault c30f3ac
fix build
fpbrault 8e83b8f
css-in-js
fpbrault 5dd7d44
Revert "css-in-js"
fpbrault f204a98
remove logs
fpbrault ca1dacc
remove typo
fpbrault 734364b
Add generated files
4da587b
add inline styles
fpbrault 2fe7bc1
add back atomic-text for demo
fpbrault 397bc68
fix build
fpbrault 27477e2
fix escaping
fpbrault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
export {}; | ||
import {AtomicText as LitAtomicText} from '@coveo/atomic/components'; | ||
import {createComponent} from '@lit/react'; | ||
import React from 'react'; | ||
|
||
export const AtomicText = createComponent({ | ||
tagName: 'atomic-text', | ||
react: React, | ||
elementClass: LitAtomicText, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import autoprefixer from 'autoprefixer'; | ||
import postcssImport from 'postcss-import'; | ||
import tailwindcss from 'tailwindcss'; | ||
|
||
export default { | ||
plugins: [postcssImport(), tailwindcss(), autoprefixer()], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {readdir, mkdir, readFile, writeFile} from 'fs'; | ||
import * as lightningcss from 'lightningcss'; | ||
import {join, dirname, relative} from 'path'; | ||
import postcss from 'postcss'; | ||
import postcssLoadConfig from 'postcss-load-config'; | ||
import {fileURLToPath} from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
|
||
const srcDir = join(__dirname, 'src'); | ||
const distDir = join(__dirname, 'dist', 'components'); | ||
|
||
async function processAndMinifyCss(content, filename) { | ||
const {plugins, options} = await postcssLoadConfig(); | ||
const result = await postcss(plugins).process(content, { | ||
...options, | ||
from: filename, | ||
}); | ||
|
||
let processedCss = minifyCss(result, filename); | ||
|
||
return processedCss; | ||
} | ||
|
||
function minifyCss(result, filename) { | ||
return lightningcss | ||
.transform({ | ||
code: Buffer.from(result.css), | ||
minify: true, | ||
sourceMap: true, | ||
filename: filename, | ||
}) | ||
.code.toString(); | ||
} | ||
|
||
function escapeString(str) { | ||
return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$'); | ||
} | ||
|
||
function convertCssToJs(srcPath, distPath, file) { | ||
readFile(srcPath, 'utf8', async (err, data) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
const processedCss = await processAndMinifyCss(data, srcPath); | ||
const jsContent = `export default \`${escapeString(processedCss)}\`;`; | ||
const jsPath = distPath + '.js'; | ||
|
||
writeFile(jsPath, jsContent, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function processCssFiles(srcDir, distDir) { | ||
readdir(srcDir, {withFileTypes: true}, (err, files) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
files.forEach((file) => { | ||
const srcPath = join(srcDir, file.name); | ||
|
||
if (file.isDirectory()) { | ||
processCssFiles(srcPath, join(distDir, file.name)); | ||
} else if (file.isFile() && file.name.endsWith('.tw.css')) { | ||
const relPath = relative(srcDir, srcPath); | ||
const distPath = join(distDir, relPath); | ||
const targetDir = dirname(distPath); | ||
console.log(`Processing ${srcPath}`); | ||
|
||
mkdir(targetDir, {recursive: true}, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
convertCssToJs(srcPath, distPath, file); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
processCssFiles(srcDir, distDir); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/atomic/src/components/search/atomic-text/atomic-text.styles.tw.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
div { | ||
transition: background-color 0.3s; | ||
|
||
@apply text-white; | ||
} | ||
|
||
div:hover { | ||
@apply bg-primary-dark; | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These styles are just to demonstrate how to combine normal css, inline styles, tailwind @apply rules and tailwind classes directly in the component markup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Tailwind V4, the theme need to be imported with the theme(reference) rule so that the tailwind utilities (including our custom theme) are available. See this for example:
https://github.com/coveo/headless-lit/blob/tsc-tw4/lito/src/components/lito-result-title/lito-result-title.css
(Notice that the theme.css file in that example only contains @theme rules, as other rules cannot be referenced that way)
It is possible to import the theme directly or even tailwind itself instead, but that will result in all their css being embedded in this file.