Skip to content

Commit

Permalink
feat: include custom fonts in the reset css
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 30, 2022
1 parent 05d6512 commit 9011241
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion data/reset.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/css.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::borrow::Cow;

pub fn format_css(forms: bool, fonts_sans: &[Cow<str>], fonts_mono: &[Cow<str>]) -> String {
let reset = format!(
include_str!("../data/reset.css"),
fonts_sans.join(","),
fonts_mono.join(",")
);
if forms {
format!("{}{}", reset, include_str!("../data/form.css"))
} else {
reset
}
}
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// bug in swc
#![allow(clippy::not_unsafe_ptr_arg_deref)]

mod css;
#[cfg(test)]
mod test;

Expand All @@ -29,9 +30,6 @@ use swc_core::{
use tailwind_config::TailwindConfig;
use tailwind_parse::Directive;

static RESET_CSS: &str = include_str!("../data/reset.css");
static FORM_CSS: &str = include_str!("../data/form.css");

#[derive(serde::Deserialize, Debug)]
pub struct AppConfig<'a> {
#[serde(borrow)]
Expand Down Expand Up @@ -155,7 +153,12 @@ impl<'a> VisitMut for TransformVisitor<'a> {
*/
fn visit_mut_jsx_opening_element(&mut self, n: &mut JSXOpeningElement) {
if self.tw_style_imported && let JSXElementName::Ident(i) = &n.name && i.sym.eq("TailwindStyle") {
let atom: Atom = format!("{}{}", RESET_CSS, FORM_CSS).into();

let atom: Atom = css::format_css(
true,
self.config.theme.font_family.get("sans").map(|v| v.as_slice()).unwrap_or(&[]),
self.config.theme.font_family.get("mono").map(|v| v.as_slice()).unwrap_or(&[])
).into();

n.name = JSXElementName::Ident(Ident::new("Global".into(), i.span));
n.attrs.push(JSXAttrOrSpread::JSXAttr(
Expand Down

0 comments on commit 9011241

Please sign in to comment.