1use std::{
16 fmt,
17 hash::{Hash, Hasher},
18 rc::Rc,
19 sync::{Arc, LazyLock},
20};
21
22use super::{IntoReactive, Reactive, SharedString};
23use crate::{
24 BlendMode, BoxShadow, Cursor, FontVariant, ObjectPosition, TransformOrigin, TransitionProps,
25 color::{Hsla, Rgba, transparent_black},
26 effects::{Filter, GradientKind, GradientSize, KeyframeAnimation, Keyframes},
27 geometry::{
28 AbsoluteLength, DefiniteLength, Length, Percentage, Points, REM_SIZE, Radians, Rems, deg,
29 percentage, px, relative,
30 },
31 style::{Color, LineHeight, PositionAxis, PropertyValue, StyleKey},
32 tailwind::{self, ClassList, Decl, Part, Rank, Rule, rank, state},
33 transform::TransformKind,
34};
35
36fn points(length: AbsoluteLength) -> f32 {
38 length.to_points(REM_SIZE).0
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43pub struct FontWeight(pub u16);
44
45impl FontWeight {
46 pub const THIN: Self = Self(100);
48 pub const EXTRA_LIGHT: Self = Self(200);
50 pub const LIGHT: Self = Self(300);
52 pub const NORMAL: Self = Self(400);
54 pub const MEDIUM: Self = Self(500);
56 pub const SEMIBOLD: Self = Self(600);
58 pub const BOLD: Self = Self(700);
60 pub const EXTRA_BOLD: Self = Self(800);
62 pub const BLACK: Self = Self(900);
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
70pub struct AdaptiveColor {
71 pub light: Hsla,
73 pub dark: Hsla,
75}
76
77impl AdaptiveColor {
78 pub const TRANSPARENT: Self = Self {
80 light: transparent_black(),
81 dark: transparent_black(),
82 };
83
84 pub fn new(light: impl Into<Hsla>, dark: impl Into<Hsla>) -> Self {
86 Self {
87 light: light.into(),
88 dark: dark.into(),
89 }
90 }
91
92 pub const fn rgb(light: u32, dark: u32) -> Self {
95 Self {
96 light: crate::color::rgb(light).to_hsla(),
97 dark: crate::color::rgb(dark).to_hsla(),
98 }
99 }
100
101 pub fn opacity(self, opacity: f32) -> Self {
103 self.map(|c| c.opacity(opacity))
104 }
105
106 pub fn map(self, f: impl Fn(Hsla) -> Hsla) -> Self {
108 Self {
109 light: f(self.light),
110 dark: f(self.dark),
111 }
112 }
113}
114
115impl From<Hsla> for AdaptiveColor {
116 fn from(c: Hsla) -> Self {
117 Self { light: c, dark: c }
118 }
119}
120
121impl From<Rgba> for AdaptiveColor {
122 fn from(c: Rgba) -> Self {
123 Hsla::from(c).into()
124 }
125}
126
127impl From<Color> for AdaptiveColor {
128 fn from(c: Color) -> Self {
129 Hsla::from(c).into()
130 }
131}
132
133#[derive(Debug, Clone, Copy)]
135pub(crate) struct Bits(f32);
136
137impl PartialEq for Bits {
138 fn eq(&self, other: &Self) -> bool {
139 self.0.to_bits() == other.0.to_bits()
140 }
141}
142
143impl Eq for Bits {}
144
145impl Hash for Bits {
146 fn hash<H: Hasher>(&self, state: &mut H) {
147 self.0.to_bits().hash(state)
148 }
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
157pub(crate) struct Variant {
158 states: u16,
160 group_states: u16,
163 group: u8,
166 dark: bool,
167}
168
169impl Variant {
170 pub(crate) const HOVER: Self = Self::states(state::HOVERED);
172 pub(crate) const FOCUS: Self = Self::states(state::FOCUSED);
174 pub(crate) const FOCUS_VISIBLE: Self = Self::states(state::FOCUS_VISIBLE);
176 pub(crate) const FOCUS_WITHIN: Self = Self::states(state::FOCUS_WITHIN);
178 pub(crate) const ACTIVE: Self = Self::states(state::PRESSED);
180 pub(crate) const DISABLED: Self = Self::states(state::DISABLED);
182 pub(crate) const SELECTED: Self = Self::states(state::SELECTED);
184 pub(crate) const CHECKED: Self = Self::states(state::CHECKED);
186 pub(crate) const INDETERMINATE: Self = Self::states(state::INDETERMINATE);
188 pub(crate) const EXPANDED: Self = Self::states(state::EXPANDED);
190 pub(crate) const ARIA_PRESSED: Self = Self::states(state::ARIA_PRESSED);
193 const DARK: Self = Self {
195 states: 0,
196 group_states: 0,
197 group: 0,
198 dark: true,
199 };
200
201 const fn states(states: u16) -> Self {
202 Self {
203 states,
204 group_states: 0,
205 group: 0,
206 dark: false,
207 }
208 }
209
210 fn within(self, outer: Variant) -> Variant {
212 let (group, group_states) = match (outer.group_states, self.group_states) {
213 (0, _) => (self.group, self.group_states),
214 (o, i) if i != 0 && outer.group == self.group => (outer.group, o | i),
215 (o, _) => (outer.group, o),
216 };
217 Variant {
218 states: self.states | outer.states,
219 group_states,
220 group,
221 dark: self.dark || outer.dark,
222 }
223 }
224
225 fn rank(self, group: &str) -> Rank {
228 let mut r = Rank::default();
229 let keys = [
230 (state::HOVERED, rank::HOVER),
231 (state::FOCUSED, rank::FOCUS),
232 (state::FOCUS_VISIBLE, rank::FOCUS_VISIBLE),
233 (state::FOCUS_WITHIN, rank::FOCUS_WITHIN),
234 (state::PRESSED, rank::ACTIVE),
235 (state::DISABLED, rank::DISABLED),
236 (state::SELECTED, rank::SELECTED),
237 (state::CHECKED, rank::CHECKED),
238 (state::INDETERMINATE, rank::INDETERMINATE),
239 (state::EXPANDED, rank::EXPANDED),
240 (state::ARIA_PRESSED, rank::ARIA_PRESSED),
241 (state::BUSY, rank::ARIA_BUSY),
242 ];
243 for (bit, key) in keys {
244 if self.group_states & bit != 0 {
245 r.add_group(&key, group);
246 }
247 if self.states & bit != 0 {
248 r.add(key);
249 }
250 }
251 if self.dark {
252 r.add(rank::DARK);
253 }
254 r
255 }
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
261pub(crate) enum ColorPart {
262 Shadow,
263 InsetShadow,
264 TextShadow,
265 Ring,
266 RingOffset,
267 InsetRing,
268 From,
269 Via,
270 To,
271}
272
273impl ColorPart {
274 fn part(self, c: Hsla) -> Part {
275 let c = Color::from(c);
276 match self {
279 ColorPart::Shadow => Part::ShadowColor(c),
280 ColorPart::InsetShadow => Part::InsetShadowColor(c),
281 ColorPart::TextShadow => Part::TextShadowColor(c),
282 ColorPart::Ring => Part::RingColor(c),
283 ColorPart::RingOffset => Part::RingOffsetColor(c),
284 ColorPart::InsetRing => Part::InsetRingColor(c),
285 ColorPart::From => Part::From(c, false),
287 ColorPart::Via => Part::Via(c, false),
288 ColorPart::To => Part::To(c, false),
289 }
290 }
291}
292
293#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
295pub(crate) enum GradientShape {
296 Linear(Bits),
298 Corner { right: bool, bottom: bool },
300 Radial,
302 Conic(Bits),
304}
305
306#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
309pub(crate) enum FilterFn {
310 Blur,
311 Brightness,
312 Contrast,
313 Grayscale,
314 HueRotate,
315 Invert,
316 Opacity,
317 Saturate,
318 Sepia,
319}
320
321impl FilterFn {
322 fn part(self, backdrop: bool, amount: f32) -> Part {
325 let (slot, filter) = match self {
326 FilterFn::Blur => (0, Filter::Blur(crate::geometry::px(amount))),
327 FilterFn::Brightness => (1, Filter::Brightness(amount)),
328 FilterFn::Contrast => (2, Filter::Contrast(amount)),
329 FilterFn::Grayscale => (3, Filter::Grayscale(amount)),
330 FilterFn::HueRotate => (4, Filter::HueRotate(crate::geometry::deg(amount))),
331 FilterFn::Invert => (5, Filter::Invert(amount)),
332 FilterFn::Opacity => (6, Filter::Opacity(amount)),
333 FilterFn::Saturate => (6 + u8::from(backdrop), Filter::Saturate(amount)),
334 FilterFn::Sepia => (7 + u8::from(backdrop), Filter::Sepia(amount)),
335 };
336 Part::Filter {
337 backdrop,
338 slot,
339 filter: Some(filter),
340 }
341 }
342}
343
344#[derive(Debug, Clone)]
346pub(crate) struct Layers(Arc<[BoxShadow]>);
347
348impl Layers {
349 fn bits(&self) -> impl Iterator<Item = (u32, u32, u32, u32, u32, bool)> + '_ {
350 self.0.iter().map(|l| {
351 (
352 l.offset_x.0.to_bits(),
353 l.offset_y.0.to_bits(),
354 l.blur.0.to_bits(),
355 l.spread.0.to_bits(),
356 l.color.0,
357 l.inset,
358 )
359 })
360 }
361}
362
363impl PartialEq for Layers {
364 fn eq(&self, other: &Self) -> bool {
365 self.0.len() == other.0.len() && self.bits().eq(other.bits())
366 }
367}
368
369impl Eq for Layers {}
370
371impl Hash for Layers {
372 fn hash<H: Hasher>(&self, state: &mut H) {
373 for l in self.bits() {
374 l.hash(state);
375 }
376 }
377}
378
379#[derive(Debug, Clone)]
382pub(crate) struct TextLayers(Arc<[crate::TextShadow]>);
383
384impl TextLayers {
385 fn bits(&self) -> impl Iterator<Item = (u32, u32, u32, u32)> + '_ {
386 self.0.iter().map(|l| {
387 (
388 l.offset_x.0.to_bits(),
389 l.offset_y.0.to_bits(),
390 l.radius.0.to_bits(),
391 l.color.0,
392 )
393 })
394 }
395}
396
397impl PartialEq for TextLayers {
398 fn eq(&self, other: &Self) -> bool {
399 self.0.len() == other.0.len() && self.bits().eq(other.bits())
400 }
401}
402
403impl Eq for TextLayers {}
404
405impl Hash for TextLayers {
406 fn hash<H: Hasher>(&self, state: &mut H) {
407 for l in self.bits() {
408 l.hash(state);
409 }
410 }
411}
412
413const UNDERLINE: u8 = 1;
415const LINE_THROUGH: u8 = 2;
417
418#[derive(Debug, Clone, PartialEq, Eq, Hash)]
420pub(crate) enum Declaration {
421 Value(StyleKey, PropertyValue),
423 Default(StyleKey, PropertyValue),
427 Color(StyleKey, AdaptiveColor),
428 TextPreset(&'static str),
430 Shadow(&'static str),
432 InsetShadow(&'static str),
434 TextShadow(&'static str),
436 Shadows(Layers),
438 PartColor(ColorPart, AdaptiveColor),
439 Ring(Bits),
440 RingOffset(Bits),
441 RingInset,
442 InsetRing(Bits),
443 TextShadows(TextLayers),
445 Transform(TransformKind, Bits),
448 TransformNone,
450 TranslateNone,
452 ScaleNone,
453 RotateNone,
454 Origin([(bool, Bits); 2]),
456 Gradient(GradientShape),
457 GradientNone,
458 StopPosition(u8, Bits),
461 Filter(bool, FilterFn, Bits),
462 FilterNone(bool),
463 FontVariant {
464 set: u8,
465 clear: u8,
466 },
467 Decoration(u8),
469 Transition(u8),
471 TransitionNone,
472 Duration(Bits),
473 Delay(Bits),
474 Ease([Bits; 4]),
475 EasePreset(&'static str),
477 Animate(Option<u8>),
479 Animation(u8, Bits, Bits, [Bits; 4], Option<u32>, Option<Bits>),
483 Tracks(StyleKey, u16),
485 Span(StyleKey, u16),
487 Group(&'static str),
490}
491
492static PRESETS: LazyLock<tailwind::Theme> = LazyLock::new(tailwind::Theme::default);
494
495fn color_value(key: StyleKey, c: Hsla) -> PropertyValue {
497 let c = Color::from(c);
498 match key {
499 StyleKey::Fill | StyleKey::Stroke => PropertyValue::Paint(Some(crate::SvgPaint::Color(c))),
501 _ => PropertyValue::Color(Some(c)),
502 }
503}
504
505fn origin(x: f32, y: f32) -> TransformOrigin {
507 TransformOrigin {
508 x: DefiniteLength::Fraction(Percentage(x)),
509 y: DefiniteLength::Fraction(Percentage(y)),
510 z: Points::ZERO,
511 }
512}
513
514fn full_line() -> taffy::Line<taffy::GridPlacement<String>> {
516 taffy::Line {
517 start: crate::grid::line_number(1),
518 end: crate::grid::line_number(-1),
519 }
520}
521
522fn shadow_preset(layers: Option<&Vec<BoxShadow>>) -> Arc<[BoxShadow]> {
523 layers.map_or_else(|| Arc::from([]), |l| Arc::from(l.as_slice()))
524}
525
526const KEYFRAMES: [Keyframes; 4] = [
528 Keyframes::Spin,
529 Keyframes::Ping,
530 Keyframes::Pulse,
531 Keyframes::Bounce,
532];
533
534impl Declaration {
535 fn lower(&self, out: &mut Vec<Decl>) {
536 let part = |out: &mut Vec<Decl>, p: Part| out.push(Decl::Part(p));
537 match self {
538 Declaration::Value(key, value) => {
539 out.push(Decl::Set(*key, value.clone()));
540 match key {
543 StyleKey::BorderWidth
544 | StyleKey::BorderTopWidth
545 | StyleKey::BorderRightWidth
546 | StyleKey::BorderBottomWidth
547 | StyleKey::BorderLeftWidth
548 | StyleKey::BorderStartWidth
549 | StyleKey::BorderEndWidth => {
550 out.push(Decl::CurrentColor(StyleKey::BorderColor))
551 }
552 StyleKey::OutlineWidth => out.push(Decl::CurrentColor(StyleKey::OutlineColor)),
553 _ => {}
554 }
555 }
556 Declaration::Default(key, value) => {
557 out.push(Decl::Default(*key, value.clone()));
558 if *key == StyleKey::OutlineWidth {
559 out.push(Decl::CurrentColor(StyleKey::OutlineColor));
560 }
561 }
562 Declaration::Color(key, c) => out.push(if c.light == c.dark {
563 Decl::Set(*key, color_value(*key, c.light))
564 } else {
565 Decl::Adaptive(
566 *key,
567 [color_value(*key, c.light), color_value(*key, c.dark)],
568 )
569 }),
570 Declaration::TextPreset(name) => {
571 if let Some(&(size, line)) = PRESETS.text.get(*name) {
572 out.push(Decl::Set(
573 StyleKey::FontSize,
574 PropertyValue::OptPoints(Some(px(size))),
575 ));
576 if let Some(m) = line {
578 out.push(Decl::Default(
579 StyleKey::LineHeight,
580 PropertyValue::LineHeight(LineHeight::Multiple(m)),
581 ));
582 }
583 }
584 }
585 Declaration::Shadow(name) => {
586 part(out, Part::Shadow(shadow_preset(PRESETS.shadows.get(*name))))
587 }
588 Declaration::InsetShadow(name) => part(
589 out,
590 Part::InsetShadow(shadow_preset(PRESETS.inset_shadows.get(*name))),
591 ),
592 Declaration::TextShadow(name) => {
593 let layers = PRESETS.text_shadows.get(*name);
594 part(
595 out,
596 Part::TextShadow(
597 layers.map_or_else(|| Arc::from([]), |l| Arc::from(l.as_slice())),
598 ),
599 )
600 }
601 Declaration::Shadows(layers) => part(out, Part::Shadow(layers.0.clone())),
602 Declaration::PartColor(which, c) => out.push(if c.light == c.dark {
603 Decl::Part(which.part(c.light))
604 } else {
605 Decl::AdaptivePart(Box::new([which.part(c.light), which.part(c.dark)]))
606 }),
607 Declaration::Ring(w) => part(out, Part::Ring(w.0)),
608 Declaration::RingOffset(w) => part(out, Part::RingOffset(w.0)),
609 Declaration::RingInset => part(out, Part::RingInset),
610 Declaration::InsetRing(w) => part(out, Part::InsetRing(w.0)),
611 Declaration::TextShadows(layers) => part(out, Part::TextShadow(layers.0.clone())),
612 Declaration::Transform(kind, v) => {
613 let v = v.0;
614 let parts: &[Part] = match kind {
615 TransformKind::Scale => &[Part::ScaleX(v), Part::ScaleY(v)],
616 TransformKind::ScaleX => &[Part::ScaleX(v)],
617 TransformKind::ScaleY => &[Part::ScaleY(v)],
618 TransformKind::TranslateX => &[Part::TranslateX(tailwind::Len::Points(v))],
619 TransformKind::TranslateY => &[Part::TranslateY(tailwind::Len::Points(v))],
620 TransformKind::TranslateXFraction => {
621 &[Part::TranslateX(tailwind::Len::Fraction(v))]
622 }
623 TransformKind::TranslateYFraction => {
624 &[Part::TranslateY(tailwind::Len::Fraction(v))]
625 }
626 TransformKind::Rotate => &[Part::Rotate(v)],
627 TransformKind::RotateX => &[Part::RotateX(v)],
628 TransformKind::RotateY => &[Part::RotateY(v)],
629 TransformKind::SkewX => &[Part::SkewX(v)],
630 TransformKind::SkewY => &[Part::SkewY(v)],
631 TransformKind::Perspective => &[Part::Perspective(Some(v))],
632 };
633 out.extend(parts.iter().cloned().map(Decl::Part));
634 }
635 Declaration::TransformNone => part(out, Part::TransformNone),
636 Declaration::TranslateNone => part(out, Part::TranslateNone),
637 Declaration::ScaleNone => part(out, Part::ScaleNone),
638 Declaration::RotateNone => part(out, Part::RotateNone),
639 Declaration::Origin(axes) => {
640 let [x, y] = axes.map(|(fraction, v)| {
641 if fraction {
642 DefiniteLength::Fraction(Percentage(v.0))
643 } else {
644 DefiniteLength::Absolute(AbsoluteLength::Points(px(v.0)))
645 }
646 });
647 out.push(Decl::Set(
648 StyleKey::TransformOrigin,
649 PropertyValue::TransformOrigin(TransformOrigin {
650 x,
651 y,
652 z: Points::ZERO,
653 }),
654 ));
655 }
656 Declaration::Gradient(shape) => part(
657 out,
658 Part::Gradient(match *shape {
659 GradientShape::Linear(angle) => GradientKind::Linear {
660 angle: Radians(angle.0),
661 },
662 GradientShape::Corner { right, bottom } => {
663 GradientKind::LinearCorner { right, bottom }
664 }
665 GradientShape::Radial => GradientKind::Radial {
666 circle: false,
667 size: GradientSize::FarthestCorner,
668 x: percentage(0.5),
669 y: percentage(0.5),
670 },
671 GradientShape::Conic(from) => GradientKind::Conic {
672 from: Radians(from.0),
673 x: percentage(0.5),
674 y: percentage(0.5),
675 },
676 }),
677 ),
678 Declaration::GradientNone => part(out, Part::GradientNone),
679 Declaration::StopPosition(i, at) => part(
680 out,
681 match i {
682 0 => Part::FromPos(at.0),
683 1 => Part::ViaPos(at.0),
684 _ => Part::ToPos(at.0),
685 },
686 ),
687 Declaration::Filter(backdrop, f, amount) => part(out, f.part(*backdrop, amount.0)),
688 Declaration::FilterNone(backdrop) => part(
689 out,
690 Part::FilterNone {
691 backdrop: *backdrop,
692 },
693 ),
694 Declaration::FontVariant { set, clear } => part(
695 out,
696 Part::FontVariant {
697 set: *set,
698 clear: *clear,
699 },
700 ),
701 Declaration::Decoration(bits) => out.push(Decl::Set(
702 StyleKey::TextDecorationLine,
703 PropertyValue::TextDecoration(Some(crate::TextDecoration {
704 underline: bits & UNDERLINE != 0,
705 line_through: bits & LINE_THROUGH != 0,
706 })),
707 )),
708 Declaration::Transition(props) => part(out, Part::TransitionProps(*props)),
709 Declaration::TransitionNone => part(out, Part::TransitionNone),
710 Declaration::Duration(ms) => part(out, Part::Duration(ms.0)),
711 Declaration::Delay(ms) => part(out, Part::Delay(ms.0)),
712 Declaration::Ease(curve) => part(out, Part::Ease(curve.map(|b| b.0))),
713 Declaration::EasePreset(name) => {
714 if let Some(curve) = PRESETS.ease.get(*name) {
715 part(out, Part::Ease(*curve));
716 }
717 }
718 Declaration::Animate(k) => part(out, Part::Animate(k.map(|i| KEYFRAMES[i as usize]))),
719 Declaration::Animation(k, duration, delay, easing, iterations, frame_rate) => part(
720 out,
721 Part::Animation(KeyframeAnimation {
722 keyframes: KEYFRAMES[*k as usize],
723 duration: duration.0,
724 delay: delay.0,
725 easing: easing.map(|b| b.0),
726 iterations: *iterations,
727 frame_rate: frame_rate.map(|b| b.0),
728 }),
729 ),
730 Declaration::Tracks(key, n) => out.push(Decl::Set(
731 *key,
732 PropertyValue::grid_template(crate::grid::equal_tracks(*n)),
733 )),
734 Declaration::Span(key, n) => out.push(Decl::Set(
735 *key,
736 PropertyValue::GridLine(crate::grid::span(*n)),
737 )),
738 Declaration::Group(_) => {}
740 }
741 }
742}
743
744#[derive(Clone, Default)]
749struct Entries(Option<Rc<Vec<(Variant, Declaration)>>>);
750
751impl Entries {
752 fn with_capacity(capacity: usize) -> Self {
753 Self(Some(Rc::new(Vec::with_capacity(capacity))))
754 }
755
756 fn as_slice(&self) -> &[(Variant, Declaration)] {
757 self.0.as_deref().map_or(&[], Vec::as_slice)
758 }
759
760 fn capacity(&self) -> usize {
761 self.0.as_ref().map_or(0, |v| v.capacity())
762 }
763
764 fn reserve(&mut self, additional: usize) {
765 self.vec().reserve(additional);
766 }
767
768 fn push(&mut self, entry: (Variant, Declaration)) {
769 self.vec().push(entry);
770 }
771
772 fn extend_from_slice(&mut self, entries: &[(Variant, Declaration)]) {
773 self.vec().extend_from_slice(entries);
774 }
775
776 fn vec(&mut self) -> &mut Vec<(Variant, Declaration)> {
777 Rc::make_mut(self.0.get_or_insert_default())
778 }
779
780 fn into_vec(self) -> Vec<(Variant, Declaration)> {
781 self.0.map(Rc::unwrap_or_clone).unwrap_or_default()
782 }
783}
784
785impl std::ops::Deref for Entries {
786 type Target = [(Variant, Declaration)];
787
788 fn deref(&self) -> &Self::Target {
789 self.as_slice()
790 }
791}
792
793impl PartialEq for Entries {
794 fn eq(&self, other: &Self) -> bool {
795 match (&self.0, &other.0) {
796 (Some(a), Some(b)) if Rc::ptr_eq(a, b) => true,
797 _ => self.as_slice() == other.as_slice(),
798 }
799 }
800}
801
802impl Eq for Entries {}
803
804impl Hash for Entries {
805 fn hash<H: Hasher>(&self, state: &mut H) {
806 self.as_slice().hash(state);
807 }
808}
809
810impl fmt::Debug for Entries {
811 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
812 self.as_slice().fmt(f)
813 }
814}
815
816crate::static_reactive!(AdaptiveColor: AdaptiveColor, Hsla, Rgba, Color);
817
818macro_rules! optional_reactive_colors {
821 ($($source:ty),*) => {$(
822 impl IntoReactive<Option<AdaptiveColor>> for $source {
823 fn into_reactive(self) -> Reactive<Option<AdaptiveColor>> {
824 Reactive::Static(Some(self.into()))
825 }
826 }
827 )*};
828}
829
830optional_reactive_colors!(AdaptiveColor, Hsla, Rgba, Color);
831
832crate::static_reactive!(Length: Length, Points, Rems, AbsoluteLength, DefiniteLength, Percentage);
833crate::static_reactive!(DefiniteLength: Points, Rems, AbsoluteLength, DefiniteLength, Percentage);
834crate::static_reactive!(AbsoluteLength: Points, Rems, AbsoluteLength);
835crate::static_reactive!(Points: Points);
836crate::static_reactive!(LineHeight: LineHeight, Points, Rems, AbsoluteLength);
837crate::static_reactive!(Radians: Radians);
838crate::static_reactive!(Percentage: Percentage);
839crate::static_reactive!(u16: u16);
840crate::static_reactive!(i16: i16);
841
842pub(crate) type BoundStyle = Rc<dyn Fn(&mut StyleRefinement)>;
845
846#[derive(Clone, Default)]
851pub struct StyleRefinement {
852 entries: Entries,
853 names: Vec<&'static str>,
855 bound: Vec<BoundStyle>,
856}
857
858impl fmt::Debug for StyleRefinement {
859 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
860 f.debug_struct("StyleRefinement")
861 .field("entries", &self.entries)
862 .field("names", &self.names)
863 .field("bound", &self.bound.len())
864 .finish()
865 }
866}
867
868impl PartialEq for StyleRefinement {
870 fn eq(&self, other: &Self) -> bool {
871 self.entries == other.entries
872 && self.names == other.names
873 && self.bound.len() == other.bound.len()
874 && self
875 .bound
876 .iter()
877 .zip(&other.bound)
878 .all(|(a, b)| Rc::ptr_eq(a, b))
879 }
880}
881
882impl Eq for StyleRefinement {}
883
884impl Hash for StyleRefinement {
887 fn hash<H: Hasher>(&self, state: &mut H) {
888 self.entries.hash(state);
889 self.names.hash(state);
890 }
891}
892
893impl StyleRefinement {
894 pub(crate) fn bind_value<T: 'static>(
897 &mut self,
898 value: impl IntoReactive<T>,
899 apply: impl Fn(&mut StyleRefinement, T) + 'static,
900 ) {
901 match value.into_reactive() {
902 Reactive::Static(v) => apply(self, v),
903 Reactive::Bound(f) => self.bound.push(Rc::new(move |s| apply(s, f()))),
904 }
905 }
906
907 pub(crate) fn bind(&mut self, part: impl Fn(&mut StyleRefinement) + 'static) {
909 self.bound.push(Rc::new(part));
910 }
911
912 pub(crate) fn is_bound(&self) -> bool {
914 !self.bound.is_empty()
915 }
916
917 pub(crate) fn static_part(&self) -> StyleRefinement {
919 StyleRefinement {
920 entries: self.entries.clone(),
921 names: self.names.clone(),
922 bound: Vec::new(),
923 }
924 }
925
926 pub(crate) fn resolved(&self) -> StyleRefinement {
929 let entries = if self.bound.is_empty() {
933 self.entries.clone()
934 } else {
935 let mut entries = Entries::with_capacity(self.entries.len() + self.bound.len());
936 entries.extend_from_slice(&self.entries);
937 entries
938 };
939 let mut out = StyleRefinement {
940 entries,
941 names: self.names.clone(),
942 bound: Vec::new(),
943 };
944 for part in &self.bound {
945 part(&mut out);
946 }
947 out
948 }
949
950 pub(crate) fn push(&mut self, decl: Declaration) {
951 if self.entries.capacity() == 0 {
953 self.entries.reserve(8);
954 }
955 self.entries.push((Variant::default(), decl));
956 }
957
958 pub(crate) fn lengths(&mut self, keys: &[StyleKey], length: impl Into<Length>) {
959 let length = length.into();
960 for key in keys {
961 self.checked(*key, key.length_value(length));
962 }
963 }
964
965 pub(crate) fn number(&mut self, key: StyleKey, n: f32) {
966 self.checked(key, key.number_value(n));
967 }
968
969 pub(crate) fn numbers(&mut self, keys: &[StyleKey], n: f32) {
970 for key in keys {
971 self.number(*key, n);
972 }
973 }
974
975 pub(crate) fn env_plus(&mut self, key: StyleKey, name: &str, extra: AbsoluteLength) {
977 match crate::style::EnvVar::parse(name) {
978 Some(var) => self.value(key, PropertyValue::EnvLength(px(points(extra)), var)),
979 None => debug_assert!(false, "no environment variable `{name}`"),
980 }
981 }
982
983 pub(crate) fn env(&mut self, key: StyleKey, var: crate::style::EnvVar) {
985 self.value(key, PropertyValue::EnvLength(Points::ZERO, var));
986 }
987
988 pub(crate) fn value(&mut self, key: StyleKey, value: PropertyValue) {
989 self.push(Declaration::Value(key, value));
990 }
991
992 fn checked(&mut self, key: StyleKey, value: Result<PropertyValue, crate::StyleError>) {
995 match value {
996 Ok(value) => self.value(key, value),
997 Err(_) => debug_assert!(false, "an invalid typed value of `{}`", key.name()),
998 }
999 }
1000
1001 fn css(&mut self, key: StyleKey, value: &str) {
1004 match key.parse_value(crate::StyleValue::Str(value)) {
1005 Ok(value) => self.value(key, value),
1006 Err(_) => log::warn!("invalid value for style `{}`: {value}", key.name()),
1007 }
1008 }
1009
1010 pub(crate) fn color(&mut self, keys: &[StyleKey], color: AdaptiveColor) {
1011 for key in keys {
1012 self.push(Declaration::Color(*key, color));
1013 }
1014 }
1015
1016 pub(crate) fn extend_as(&mut self, variant: Variant, other: StyleRefinement) {
1019 for (mut v, d) in other.entries.into_vec() {
1020 if v.group != 0 {
1021 v.group = self.name(other.names[v.group as usize - 1]);
1022 }
1023 self.entries.push((v.within(variant), d));
1024 }
1025 for part in other.bound {
1027 self.bound.push(Rc::new(move |s| {
1028 let mut inner = StyleRefinement::default();
1029 part(&mut inner);
1030 s.extend_as(variant, inner);
1031 }));
1032 }
1033 }
1034
1035 fn name(&mut self, name: &'static str) -> u8 {
1037 if name.is_empty() {
1038 return 0;
1039 }
1040 let at = match self.names.iter().position(|n| *n == name) {
1041 Some(at) => at,
1042 None => {
1043 self.names.push(name);
1044 self.names.len() - 1
1045 }
1046 };
1047 u8::try_from(at + 1).expect("fewer than 255 group names")
1048 }
1049
1050 pub(crate) fn static_color(&self, key: StyleKey) -> Option<AdaptiveColor> {
1053 self.entries.iter().rev().find_map(|(variant, d)| {
1054 if *variant != Variant::default() {
1055 return None;
1056 }
1057 match d {
1058 Declaration::Color(k, color) if *k == key => Some(Some(*color)),
1059 Declaration::Value(k, PropertyValue::Color(color)) if *k == key => {
1060 Some(color.map(AdaptiveColor::from))
1061 }
1062 _ => None,
1063 }
1064 })?
1065 }
1066
1067 pub fn is_empty(&self) -> bool {
1069 self.entries.is_empty() && self.bound.is_empty()
1070 }
1071
1072 pub(crate) fn lower(&self) -> ClassList {
1075 let mut variants: Vec<Variant> = Vec::new();
1076 let mut groups: Vec<Box<str>> = Vec::new();
1077 for (v, d) in self.entries.iter() {
1078 if let Declaration::Group(name) = d {
1079 if !groups.iter().any(|g| **g == **name) {
1080 groups.push((*name).into());
1081 }
1082 } else if !variants.contains(v) {
1083 variants.push(*v);
1084 }
1085 }
1086 let mut rules = Vec::with_capacity(variants.len());
1087 for variant in variants {
1088 let mut decls = Vec::new();
1089 let mut decoration = 0;
1092 for (v, d) in self.entries.iter() {
1093 if *v != variant {
1094 continue;
1095 }
1096 if let Declaration::Decoration(bits) = d {
1097 decoration = if *bits == 0 { 0 } else { decoration | bits };
1098 Declaration::Decoration(decoration).lower(&mut decls);
1099 } else {
1100 d.lower(&mut decls);
1101 }
1102 }
1103 if !decls.is_empty() {
1104 let name = match variant.group {
1105 0 => "",
1106 g => self.names[g as usize - 1],
1107 };
1108 let group = (variant.group_states != 0).then_some((name, variant.group_states));
1109 rules.push(Rule::with(
1110 variant.states,
1111 group,
1112 variant.dark,
1113 variant.rank(name),
1114 decls,
1115 ));
1116 }
1117 }
1118 ClassList::from_rules(rules, groups)
1119 }
1120}
1121
1122fn font_variant<S: Styled>(mut s: S, set: u8, clear: u8) -> S {
1124 s.style().push(Declaration::FontVariant { set, clear });
1125 s
1126}
1127
1128#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1130pub enum WritingDirection {
1131 #[default]
1133 Auto,
1134 Ltr,
1136 Rtl,
1138}
1139
1140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1142pub enum LayoutDirection {
1143 #[default]
1146 Inherit,
1147 Ltr,
1149 Rtl,
1152}
1153crate::static_reactive!(LayoutDirection: LayoutDirection);
1154
1155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1157pub enum BorderCurve {
1158 #[default]
1160 Circular,
1161 Continuous,
1163}
1164
1165pub trait Styled: Sized {
1169 fn style(&mut self) -> &mut StyleRefinement;
1171
1172 nsh_macros::style_helpers! {
1173 p => [Padding],
1175 px => [PaddingHorizontal],
1177 py => [PaddingVertical],
1179 pt => [PaddingTop],
1181 pr => [PaddingRight],
1183 pb => [PaddingBottom],
1185 pl => [PaddingLeft],
1187 ps => [PaddingStart],
1189 pe => [PaddingEnd],
1191 m => [Margin],
1193 mx => [MarginHorizontal],
1195 my => [MarginVertical],
1197 mt => [MarginTop],
1199 mr => [MarginRight],
1201 mb => [MarginBottom],
1203 ml => [MarginLeft],
1205 ms => [MarginStart],
1207 me => [MarginEnd],
1209 gap => [Gap],
1211 gap_x => [ColumnGap],
1213 gap_y => [RowGap],
1215 w => [Width],
1217 h => [Height],
1219 size => [Width, Height],
1221 min_w => [MinWidth],
1223 min_h => [MinHeight],
1225 max_w => [MaxWidth],
1227 max_h => [MaxHeight],
1229 top => [Top],
1231 right => [Right],
1233 bottom => [Bottom],
1235 left => [Left],
1237 inset => [Top, Right, Bottom, Left],
1239 basis => [FlexBasis],
1241 }
1242
1243 fn p_safe_area(mut self) -> Self {
1250 let s = self.style();
1251 s.env(StyleKey::PaddingTop, crate::style::EnvVar::SafeAreaInsetTop);
1252 s.env(
1253 StyleKey::PaddingRight,
1254 crate::style::EnvVar::SafeAreaInsetRight,
1255 );
1256 s.env(
1257 StyleKey::PaddingBottom,
1258 crate::style::EnvVar::SafeAreaInsetBottom,
1259 );
1260 s.env(
1261 StyleKey::PaddingLeft,
1262 crate::style::EnvVar::SafeAreaInsetLeft,
1263 );
1264 self
1265 }
1266
1267 fn px_safe_area(mut self) -> Self {
1270 let s = self.style();
1271 s.env(
1272 StyleKey::PaddingRight,
1273 crate::style::EnvVar::SafeAreaInsetRight,
1274 );
1275 s.env(
1276 StyleKey::PaddingLeft,
1277 crate::style::EnvVar::SafeAreaInsetLeft,
1278 );
1279 self
1280 }
1281
1282 fn py_safe_area(mut self) -> Self {
1285 let s = self.style();
1286 s.env(StyleKey::PaddingTop, crate::style::EnvVar::SafeAreaInsetTop);
1287 s.env(
1288 StyleKey::PaddingBottom,
1289 crate::style::EnvVar::SafeAreaInsetBottom,
1290 );
1291 self
1292 }
1293
1294 fn pt_safe_area(mut self) -> Self {
1297 self.style()
1298 .env(StyleKey::PaddingTop, crate::style::EnvVar::SafeAreaInsetTop);
1299 self
1300 }
1301
1302 fn pt_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1305 self.style()
1306 .env_plus(StyleKey::PaddingTop, "safe-area-inset-top", extra.into());
1307 self
1308 }
1309
1310 fn pr_safe_area(mut self) -> Self {
1313 self.style().env(
1314 StyleKey::PaddingRight,
1315 crate::style::EnvVar::SafeAreaInsetRight,
1316 );
1317 self
1318 }
1319
1320 fn pr_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1323 self.style().env_plus(
1324 StyleKey::PaddingRight,
1325 "safe-area-inset-right",
1326 extra.into(),
1327 );
1328 self
1329 }
1330
1331 fn pb_safe_area(mut self) -> Self {
1334 self.style().env(
1335 StyleKey::PaddingBottom,
1336 crate::style::EnvVar::SafeAreaInsetBottom,
1337 );
1338 self
1339 }
1340
1341 fn pb_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1344 self.style().env_plus(
1345 StyleKey::PaddingBottom,
1346 "safe-area-inset-bottom",
1347 extra.into(),
1348 );
1349 self
1350 }
1351
1352 fn pl_safe_area(mut self) -> Self {
1355 self.style().env(
1356 StyleKey::PaddingLeft,
1357 crate::style::EnvVar::SafeAreaInsetLeft,
1358 );
1359 self
1360 }
1361
1362 fn pl_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1365 self.style()
1366 .env_plus(StyleKey::PaddingLeft, "safe-area-inset-left", extra.into());
1367 self
1368 }
1369
1370 fn pb_keyboard(mut self) -> Self {
1375 self.style().env(
1376 StyleKey::PaddingBottom,
1377 crate::style::EnvVar::KeyboardInsetHeight,
1378 );
1379 self
1380 }
1381
1382 fn pb_keyboard_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1386 self.style().env_plus(
1387 StyleKey::PaddingBottom,
1388 "keyboard-inset-height",
1389 extra.into(),
1390 );
1391 self
1392 }
1393
1394 fn m_safe_area(mut self) -> Self {
1399 let s = self.style();
1400 s.env(StyleKey::MarginTop, crate::style::EnvVar::SafeAreaInsetTop);
1401 s.env(
1402 StyleKey::MarginRight,
1403 crate::style::EnvVar::SafeAreaInsetRight,
1404 );
1405 s.env(
1406 StyleKey::MarginBottom,
1407 crate::style::EnvVar::SafeAreaInsetBottom,
1408 );
1409 s.env(
1410 StyleKey::MarginLeft,
1411 crate::style::EnvVar::SafeAreaInsetLeft,
1412 );
1413 self
1414 }
1415
1416 fn mx_safe_area(mut self) -> Self {
1419 let s = self.style();
1420 s.env(
1421 StyleKey::MarginRight,
1422 crate::style::EnvVar::SafeAreaInsetRight,
1423 );
1424 s.env(
1425 StyleKey::MarginLeft,
1426 crate::style::EnvVar::SafeAreaInsetLeft,
1427 );
1428 self
1429 }
1430
1431 fn my_safe_area(mut self) -> Self {
1434 let s = self.style();
1435 s.env(StyleKey::MarginTop, crate::style::EnvVar::SafeAreaInsetTop);
1436 s.env(
1437 StyleKey::MarginBottom,
1438 crate::style::EnvVar::SafeAreaInsetBottom,
1439 );
1440 self
1441 }
1442
1443 fn mt_safe_area(mut self) -> Self {
1446 self.style()
1447 .env(StyleKey::MarginTop, crate::style::EnvVar::SafeAreaInsetTop);
1448 self
1449 }
1450
1451 fn mt_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1454 self.style()
1455 .env_plus(StyleKey::MarginTop, "safe-area-inset-top", extra.into());
1456 self
1457 }
1458
1459 fn mr_safe_area(mut self) -> Self {
1462 self.style().env(
1463 StyleKey::MarginRight,
1464 crate::style::EnvVar::SafeAreaInsetRight,
1465 );
1466 self
1467 }
1468
1469 fn mr_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1472 self.style()
1473 .env_plus(StyleKey::MarginRight, "safe-area-inset-right", extra.into());
1474 self
1475 }
1476
1477 fn mb_safe_area(mut self) -> Self {
1480 self.style().env(
1481 StyleKey::MarginBottom,
1482 crate::style::EnvVar::SafeAreaInsetBottom,
1483 );
1484 self
1485 }
1486
1487 fn mb_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1490 self.style().env_plus(
1491 StyleKey::MarginBottom,
1492 "safe-area-inset-bottom",
1493 extra.into(),
1494 );
1495 self
1496 }
1497
1498 fn ml_safe_area(mut self) -> Self {
1501 self.style().env(
1502 StyleKey::MarginLeft,
1503 crate::style::EnvVar::SafeAreaInsetLeft,
1504 );
1505 self
1506 }
1507
1508 fn ml_safe_area_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1511 self.style()
1512 .env_plus(StyleKey::MarginLeft, "safe-area-inset-left", extra.into());
1513 self
1514 }
1515
1516 fn mb_keyboard(mut self) -> Self {
1521 self.style().env(
1522 StyleKey::MarginBottom,
1523 crate::style::EnvVar::KeyboardInsetHeight,
1524 );
1525 self
1526 }
1527
1528 fn mb_keyboard_plus(mut self, extra: impl Into<AbsoluteLength>) -> Self {
1532 self.style().env_plus(
1533 StyleKey::MarginBottom,
1534 "keyboard-inset-height",
1535 extra.into(),
1536 );
1537 self
1538 }
1539
1540 fn flex(mut self) -> Self {
1546 let s = self.style();
1547 s.value(
1548 StyleKey::Display,
1549 PropertyValue::Display(taffy::Display::Flex),
1550 );
1551 s.push(Declaration::Default(
1552 StyleKey::FlexDirection,
1553 PropertyValue::FlexDirection(taffy::FlexDirection::Row),
1554 ));
1555 self
1556 }
1557
1558 fn grid(mut self) -> Self {
1560 self.style().value(
1561 StyleKey::Display,
1562 PropertyValue::Display(taffy::Display::Grid),
1563 );
1564 self
1565 }
1566
1567 fn grid_cols(mut self, count: impl IntoReactive<u16>) -> Self {
1569 self.style().bind_value(count, |s, count: u16| {
1570 s.push(Declaration::Tracks(StyleKey::GridTemplateColumns, count));
1571 });
1572 self
1573 }
1574
1575 fn grid_rows(mut self, count: impl IntoReactive<u16>) -> Self {
1577 self.style().bind_value(count, |s, count: u16| {
1578 s.push(Declaration::Tracks(StyleKey::GridTemplateRows, count));
1579 });
1580 self
1581 }
1582
1583 fn grid_template_columns(mut self, tracks: impl IntoReactive<SharedString>) -> Self {
1586 self.style().bind_value(tracks, |s, tracks: SharedString| {
1587 s.css(StyleKey::GridTemplateColumns, &tracks);
1588 });
1589 self
1590 }
1591
1592 fn grid_template_rows(mut self, tracks: impl IntoReactive<SharedString>) -> Self {
1594 self.style().bind_value(tracks, |s, tracks: SharedString| {
1595 s.css(StyleKey::GridTemplateRows, &tracks);
1596 });
1597 self
1598 }
1599
1600 fn grid_flow_col(mut self) -> Self {
1602 self.style().value(
1603 StyleKey::GridAutoFlow,
1604 PropertyValue::GridAutoFlow(taffy::GridAutoFlow::Column),
1605 );
1606 self
1607 }
1608
1609 fn grid_flow_dense(mut self) -> Self {
1611 self.style().value(
1612 StyleKey::GridAutoFlow,
1613 PropertyValue::GridAutoFlow(taffy::GridAutoFlow::RowDense),
1614 );
1615 self
1616 }
1617
1618 fn col_span(mut self, count: impl IntoReactive<u16>) -> Self {
1620 self.style().bind_value(count, |s, count: u16| {
1621 s.push(Declaration::Span(StyleKey::GridColumn, count));
1622 });
1623 self
1624 }
1625
1626 fn row_span(mut self, count: impl IntoReactive<u16>) -> Self {
1628 self.style().bind_value(count, |s, count: u16| {
1629 s.push(Declaration::Span(StyleKey::GridRow, count));
1630 });
1631 self
1632 }
1633
1634 fn col_span_full(mut self) -> Self {
1636 self.style()
1637 .value(StyleKey::GridColumn, PropertyValue::GridLine(full_line()));
1638 self
1639 }
1640
1641 fn row_span_full(mut self) -> Self {
1643 self.style()
1644 .value(StyleKey::GridRow, PropertyValue::GridLine(full_line()));
1645 self
1646 }
1647
1648 fn col_start(mut self, line: impl IntoReactive<i16>) -> Self {
1651 self.style().bind_value(line, |s, line: i16| {
1652 s.number(StyleKey::GridColumnStart, f32::from(line));
1653 });
1654 self
1655 }
1656
1657 fn col_end(mut self, line: impl IntoReactive<i16>) -> Self {
1659 self.style().bind_value(line, |s, line: i16| {
1660 s.number(StyleKey::GridColumnEnd, f32::from(line));
1661 });
1662 self
1663 }
1664
1665 fn row_start(mut self, line: impl IntoReactive<i16>) -> Self {
1667 self.style().bind_value(line, |s, line: i16| {
1668 s.number(StyleKey::GridRowStart, f32::from(line));
1669 });
1670 self
1671 }
1672
1673 fn row_end(mut self, line: impl IntoReactive<i16>) -> Self {
1675 self.style().bind_value(line, |s, line: i16| {
1676 s.number(StyleKey::GridRowEnd, f32::from(line));
1677 });
1678 self
1679 }
1680
1681 fn hidden(mut self) -> Self {
1683 self.style().value(
1684 StyleKey::Display,
1685 PropertyValue::Display(taffy::Display::None),
1686 );
1687 self
1688 }
1689
1690 fn invisible(mut self) -> Self {
1696 self.style()
1697 .value(StyleKey::Visibility, PropertyValue::Bool(true));
1698 self
1699 }
1700
1701 fn flex_row(mut self) -> Self {
1703 self.style().value(
1704 StyleKey::FlexDirection,
1705 PropertyValue::FlexDirection(taffy::FlexDirection::Row),
1706 );
1707 self
1708 }
1709
1710 fn flex_row_reverse(mut self) -> Self {
1712 self.style().value(
1713 StyleKey::FlexDirection,
1714 PropertyValue::FlexDirection(taffy::FlexDirection::RowReverse),
1715 );
1716 self
1717 }
1718
1719 fn flex_col(mut self) -> Self {
1721 self.style().value(
1722 StyleKey::FlexDirection,
1723 PropertyValue::FlexDirection(taffy::FlexDirection::Column),
1724 );
1725 self
1726 }
1727
1728 fn flex_col_reverse(mut self) -> Self {
1730 self.style().value(
1731 StyleKey::FlexDirection,
1732 PropertyValue::FlexDirection(taffy::FlexDirection::ColumnReverse),
1733 );
1734 self
1735 }
1736
1737 fn flex_wrap(mut self) -> Self {
1739 self.style().value(
1740 StyleKey::FlexWrap,
1741 PropertyValue::FlexWrap(taffy::FlexWrap::Wrap),
1742 );
1743 self
1744 }
1745
1746 fn flex_1(mut self) -> Self {
1748 let s = self.style();
1749 s.number(StyleKey::FlexGrow, 1.0);
1750 s.number(StyleKey::FlexShrink, 1.0);
1751 s.lengths(&[StyleKey::FlexBasis], px(0.0));
1752 self
1753 }
1754
1755 fn flex_auto(mut self) -> Self {
1757 let s = self.style();
1758 s.number(StyleKey::FlexGrow, 1.0);
1759 s.number(StyleKey::FlexShrink, 1.0);
1760 s.lengths(&[StyleKey::FlexBasis], Length::Auto);
1761 self
1762 }
1763
1764 fn flex_none(mut self) -> Self {
1766 let s = self.style();
1767 s.number(StyleKey::FlexGrow, 0.0);
1768 s.number(StyleKey::FlexShrink, 0.0);
1769 self
1770 }
1771
1772 fn flex_grow(mut self, factor: impl IntoReactive<f32>) -> Self {
1775 self.style().bind_value(factor, |s, factor: f32| {
1776 s.number(StyleKey::FlexGrow, factor);
1777 });
1778 self
1779 }
1780
1781 fn flex_shrink(mut self, factor: impl IntoReactive<f32>) -> Self {
1784 self.style().bind_value(factor, |s, factor: f32| {
1785 s.number(StyleKey::FlexShrink, factor);
1786 });
1787 self
1788 }
1789
1790 fn flex_shrink_0(mut self) -> Self {
1792 self.style().number(StyleKey::FlexShrink, 0.0);
1793 self
1794 }
1795
1796 fn items_start(mut self) -> Self {
1798 self.style().value(
1799 StyleKey::AlignItems,
1800 PropertyValue::AlignItems(Some(taffy::AlignItems::FLEX_START)),
1801 );
1802 self
1803 }
1804
1805 fn items_center(mut self) -> Self {
1807 self.style().value(
1808 StyleKey::AlignItems,
1809 PropertyValue::AlignItems(Some(taffy::AlignItems::CENTER)),
1810 );
1811 self
1812 }
1813
1814 fn items_end(mut self) -> Self {
1816 self.style().value(
1817 StyleKey::AlignItems,
1818 PropertyValue::AlignItems(Some(taffy::AlignItems::FLEX_END)),
1819 );
1820 self
1821 }
1822
1823 fn items_stretch(mut self) -> Self {
1825 self.style().value(
1826 StyleKey::AlignItems,
1827 PropertyValue::AlignItems(Some(taffy::AlignItems::STRETCH)),
1828 );
1829 self
1830 }
1831
1832 fn items_baseline(mut self) -> Self {
1834 self.style().value(
1835 StyleKey::AlignItems,
1836 PropertyValue::AlignItems(Some(taffy::AlignItems::BASELINE)),
1837 );
1838 self
1839 }
1840
1841 fn justify_start(mut self) -> Self {
1843 self.style().value(
1844 StyleKey::JustifyContent,
1845 PropertyValue::AlignContent(taffy::AlignContent::FLEX_START),
1846 );
1847 self
1848 }
1849
1850 fn justify_center(mut self) -> Self {
1852 self.style().value(
1853 StyleKey::JustifyContent,
1854 PropertyValue::AlignContent(taffy::AlignContent::CENTER),
1855 );
1856 self
1857 }
1858
1859 fn justify_end(mut self) -> Self {
1861 self.style().value(
1862 StyleKey::JustifyContent,
1863 PropertyValue::AlignContent(taffy::AlignContent::FLEX_END),
1864 );
1865 self
1866 }
1867
1868 fn justify_between(mut self) -> Self {
1870 self.style().value(
1871 StyleKey::JustifyContent,
1872 PropertyValue::AlignContent(taffy::AlignContent::SPACE_BETWEEN),
1873 );
1874 self
1875 }
1876
1877 fn justify_around(mut self) -> Self {
1879 self.style().value(
1880 StyleKey::JustifyContent,
1881 PropertyValue::AlignContent(taffy::AlignContent::SPACE_AROUND),
1882 );
1883 self
1884 }
1885
1886 fn justify_evenly(mut self) -> Self {
1888 self.style().value(
1889 StyleKey::JustifyContent,
1890 PropertyValue::AlignContent(taffy::AlignContent::SPACE_EVENLY),
1891 );
1892 self
1893 }
1894
1895 fn content_start(mut self) -> Self {
1897 self.style().value(
1898 StyleKey::AlignContent,
1899 PropertyValue::AlignContent(taffy::AlignContent::FLEX_START),
1900 );
1901 self
1902 }
1903
1904 fn content_center(mut self) -> Self {
1906 self.style().value(
1907 StyleKey::AlignContent,
1908 PropertyValue::AlignContent(taffy::AlignContent::CENTER),
1909 );
1910 self
1911 }
1912
1913 fn content_end(mut self) -> Self {
1915 self.style().value(
1916 StyleKey::AlignContent,
1917 PropertyValue::AlignContent(taffy::AlignContent::FLEX_END),
1918 );
1919 self
1920 }
1921
1922 fn content_between(mut self) -> Self {
1924 self.style().value(
1925 StyleKey::AlignContent,
1926 PropertyValue::AlignContent(taffy::AlignContent::SPACE_BETWEEN),
1927 );
1928 self
1929 }
1930
1931 fn content_around(mut self) -> Self {
1933 self.style().value(
1934 StyleKey::AlignContent,
1935 PropertyValue::AlignContent(taffy::AlignContent::SPACE_AROUND),
1936 );
1937 self
1938 }
1939
1940 fn content_evenly(mut self) -> Self {
1942 self.style().value(
1943 StyleKey::AlignContent,
1944 PropertyValue::AlignContent(taffy::AlignContent::SPACE_EVENLY),
1945 );
1946 self
1947 }
1948
1949 fn content_stretch(mut self) -> Self {
1951 self.style().value(
1952 StyleKey::AlignContent,
1953 PropertyValue::AlignContent(taffy::AlignContent::STRETCH),
1954 );
1955 self
1956 }
1957
1958 fn self_start(mut self) -> Self {
1960 self.style().value(
1961 StyleKey::AlignSelf,
1962 PropertyValue::AlignItems(Some(taffy::AlignItems::FLEX_START)),
1963 );
1964 self
1965 }
1966
1967 fn self_center(mut self) -> Self {
1969 self.style().value(
1970 StyleKey::AlignSelf,
1971 PropertyValue::AlignItems(Some(taffy::AlignItems::CENTER)),
1972 );
1973 self
1974 }
1975
1976 fn self_end(mut self) -> Self {
1978 self.style().value(
1979 StyleKey::AlignSelf,
1980 PropertyValue::AlignItems(Some(taffy::AlignItems::FLEX_END)),
1981 );
1982 self
1983 }
1984
1985 fn self_stretch(mut self) -> Self {
1987 self.style().value(
1988 StyleKey::AlignSelf,
1989 PropertyValue::AlignItems(Some(taffy::AlignItems::STRETCH)),
1990 );
1991 self
1992 }
1993
1994 fn w_full(self) -> Self {
1996 self.w(relative(1.0))
1997 }
1998
1999 fn h_full(self) -> Self {
2001 self.h(relative(1.0))
2002 }
2003
2004 fn size_full(self) -> Self {
2006 self.size(relative(1.0))
2007 }
2008
2009 fn aspect_ratio(mut self, ratio: impl IntoReactive<f32>) -> Self {
2011 self.style().bind_value(ratio, |s, ratio: f32| {
2012 s.number(StyleKey::AspectRatio, ratio);
2013 });
2014 self
2015 }
2016
2017 fn aspect_square(self) -> Self {
2019 self.aspect_ratio(1.0)
2020 }
2021
2022 fn relative(mut self) -> Self {
2024 self.style().value(
2025 StyleKey::Position,
2026 PropertyValue::Position(taffy::Position::Relative, false, false),
2027 );
2028 self
2029 }
2030
2031 fn absolute(mut self) -> Self {
2033 self.style().value(
2034 StyleKey::Position,
2035 PropertyValue::Position(taffy::Position::Absolute, false, false),
2036 );
2037 self
2038 }
2039
2040 fn sticky(mut self) -> Self {
2046 self.style().value(
2047 StyleKey::Position,
2048 PropertyValue::Position(taffy::Position::Relative, false, true),
2049 );
2050 self
2051 }
2052
2053 fn overflow_hidden(mut self) -> Self {
2055 self.style()
2056 .value(StyleKey::Overflow, PropertyValue::Bool(true));
2057 self
2058 }
2059
2060 fn z_index(mut self, z: impl IntoReactive<i32>) -> Self {
2062 self.style()
2063 .bind_value(z, |s, z: i32| s.number(StyleKey::ZIndex, z as f32));
2064 self
2065 }
2066
2067 fn bg(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2071 self.style().bind_value(color, |s, color: AdaptiveColor| {
2072 s.color(&[StyleKey::BackgroundColor], color);
2073 });
2074 self
2075 }
2076
2077 fn opacity(mut self, opacity: impl IntoReactive<f32>) -> Self {
2079 self.style().bind_value(opacity, |s, opacity: f32| {
2080 s.number(StyleKey::Opacity, opacity.clamp(0.0, 1.0));
2081 });
2082 self
2083 }
2084
2085 fn rounded(mut self, radius: impl IntoReactive<Length>) -> Self {
2087 self.style().bind_value(radius, |s, radius: Length| {
2088 s.lengths(&[StyleKey::BorderRadius], radius);
2089 });
2090 self
2091 }
2092
2093 fn rounded_none(self) -> Self {
2095 self.rounded(px(0.0))
2096 }
2097
2098 fn rounded_xs(self) -> Self {
2100 self.rounded(px(2.0))
2101 }
2102
2103 fn rounded_sm(self) -> Self {
2105 self.rounded(px(4.0))
2106 }
2107
2108 fn rounded_md(self) -> Self {
2110 self.rounded(px(6.0))
2111 }
2112
2113 fn rounded_lg(self) -> Self {
2115 self.rounded(px(8.0))
2116 }
2117
2118 fn rounded_xl(self) -> Self {
2120 self.rounded(px(12.0))
2121 }
2122
2123 fn rounded_2xl(self) -> Self {
2125 self.rounded(px(16.0))
2126 }
2127
2128 fn rounded_3xl(self) -> Self {
2130 self.rounded(px(24.0))
2131 }
2132
2133 fn rounded_full(self) -> Self {
2135 self.rounded(px(9999.0))
2136 }
2137
2138 fn rounded_t(mut self, radius: impl IntoReactive<Length>) -> Self {
2140 self.style().bind_value(radius, |s, radius: Length| {
2141 s.lengths(
2142 &[
2143 StyleKey::BorderTopLeftRadius,
2144 StyleKey::BorderTopRightRadius,
2145 ],
2146 radius,
2147 );
2148 });
2149 self
2150 }
2151
2152 fn rounded_b(mut self, radius: impl IntoReactive<Length>) -> Self {
2154 self.style().bind_value(radius, |s, radius: Length| {
2155 s.lengths(
2156 &[
2157 StyleKey::BorderBottomLeftRadius,
2158 StyleKey::BorderBottomRightRadius,
2159 ],
2160 radius,
2161 );
2162 });
2163 self
2164 }
2165
2166 fn border(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2168 self.style().bind_value(width, |s, width: AbsoluteLength| {
2169 s.number(StyleKey::BorderWidth, points(width));
2170 });
2171 self
2172 }
2173
2174 fn border_1(self) -> Self {
2176 self.border(px(1.0))
2177 }
2178
2179 fn border_2(self) -> Self {
2181 self.border(px(2.0))
2182 }
2183
2184 fn border_t_1(mut self) -> Self {
2186 self.style().number(StyleKey::BorderTopWidth, 1.0);
2187 self
2188 }
2189
2190 fn border_b_1(mut self) -> Self {
2192 self.style().number(StyleKey::BorderBottomWidth, 1.0);
2193 self
2194 }
2195
2196 fn border_l_1(mut self) -> Self {
2198 self.style().number(StyleKey::BorderLeftWidth, 1.0);
2199 self
2200 }
2201
2202 fn border_r_1(mut self) -> Self {
2204 self.style().number(StyleKey::BorderRightWidth, 1.0);
2205 self
2206 }
2207
2208 fn border_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2210 self.style().bind_value(color, |s, color: AdaptiveColor| {
2211 s.color(&[StyleKey::BorderColor], color);
2212 });
2213 self
2214 }
2215
2216 fn border_t(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2218 self.style().bind_value(width, |s, width: AbsoluteLength| {
2219 s.number(StyleKey::BorderTopWidth, points(width));
2220 });
2221 self
2222 }
2223
2224 fn border_r(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2226 self.style().bind_value(width, |s, width: AbsoluteLength| {
2227 s.number(StyleKey::BorderRightWidth, points(width));
2228 });
2229 self
2230 }
2231
2232 fn border_b(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2234 self.style().bind_value(width, |s, width: AbsoluteLength| {
2235 s.number(StyleKey::BorderBottomWidth, points(width));
2236 });
2237 self
2238 }
2239
2240 fn border_l(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2242 self.style().bind_value(width, |s, width: AbsoluteLength| {
2243 s.number(StyleKey::BorderLeftWidth, points(width));
2244 });
2245 self
2246 }
2247
2248 fn border_x(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2250 self.style().bind_value(width, |s, width: AbsoluteLength| {
2251 s.numbers(
2252 &[StyleKey::BorderLeftWidth, StyleKey::BorderRightWidth],
2253 points(width),
2254 );
2255 });
2256 self
2257 }
2258
2259 fn border_y(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2261 self.style().bind_value(width, |s, width: AbsoluteLength| {
2262 s.numbers(
2263 &[StyleKey::BorderTopWidth, StyleKey::BorderBottomWidth],
2264 points(width),
2265 );
2266 });
2267 self
2268 }
2269
2270 fn border_s(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2272 self.style().bind_value(width, |s, width: AbsoluteLength| {
2273 s.number(StyleKey::BorderStartWidth, points(width));
2274 });
2275 self
2276 }
2277
2278 fn border_e(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2280 self.style().bind_value(width, |s, width: AbsoluteLength| {
2281 s.number(StyleKey::BorderEndWidth, points(width));
2282 });
2283 self
2284 }
2285
2286 fn border_t_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2288 self.style().bind_value(color, |s, color: AdaptiveColor| {
2289 s.color(&[StyleKey::BorderTopColor], color);
2290 });
2291 self
2292 }
2293
2294 fn border_r_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2296 self.style().bind_value(color, |s, color: AdaptiveColor| {
2297 s.color(&[StyleKey::BorderRightColor], color);
2298 });
2299 self
2300 }
2301
2302 fn border_b_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2304 self.style().bind_value(color, |s, color: AdaptiveColor| {
2305 s.color(&[StyleKey::BorderBottomColor], color);
2306 });
2307 self
2308 }
2309
2310 fn border_l_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2312 self.style().bind_value(color, |s, color: AdaptiveColor| {
2313 s.color(&[StyleKey::BorderLeftColor], color);
2314 });
2315 self
2316 }
2317
2318 fn border_x_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2320 self.style().bind_value(color, |s, color: AdaptiveColor| {
2321 s.color(
2322 &[StyleKey::BorderLeftColor, StyleKey::BorderRightColor],
2323 color,
2324 );
2325 });
2326 self
2327 }
2328
2329 fn border_y_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2331 self.style().bind_value(color, |s, color: AdaptiveColor| {
2332 s.color(
2333 &[StyleKey::BorderTopColor, StyleKey::BorderBottomColor],
2334 color,
2335 );
2336 });
2337 self
2338 }
2339
2340 fn border_s_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2342 self.style().bind_value(color, |s, color: AdaptiveColor| {
2343 s.color(&[StyleKey::BorderStartColor], color);
2344 });
2345 self
2346 }
2347
2348 fn border_e_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2350 self.style().bind_value(color, |s, color: AdaptiveColor| {
2351 s.color(&[StyleKey::BorderEndColor], color);
2352 });
2353 self
2354 }
2355
2356 fn border_solid(mut self) -> Self {
2358 self.style().value(
2359 StyleKey::BorderStyle,
2360 PropertyValue::BorderStyle(crate::BorderStyle::Solid, false),
2361 );
2362 self
2363 }
2364
2365 fn border_dashed(mut self) -> Self {
2367 self.style().value(
2368 StyleKey::BorderStyle,
2369 PropertyValue::BorderStyle(crate::BorderStyle::Dashed, false),
2370 );
2371 self
2372 }
2373
2374 fn border_dotted(mut self) -> Self {
2376 self.style().value(
2377 StyleKey::BorderStyle,
2378 PropertyValue::BorderStyle(crate::BorderStyle::Dotted, false),
2379 );
2380 self
2381 }
2382
2383 fn border_none(mut self) -> Self {
2386 self.style().value(
2387 StyleKey::BorderStyle,
2388 PropertyValue::BorderStyle(crate::BorderStyle::Solid, true),
2389 );
2390 self
2391 }
2392
2393 fn border_curve(mut self, curve: BorderCurve) -> Self {
2395 self.style().value(
2396 StyleKey::BorderCurve,
2397 PropertyValue::Bool(matches!(curve, BorderCurve::Continuous)),
2398 );
2399 self
2400 }
2401
2402 fn rounded_tl(mut self, radius: impl IntoReactive<Length>) -> Self {
2404 self.style().bind_value(radius, |s, radius: Length| {
2405 s.lengths(&[StyleKey::BorderTopLeftRadius], radius);
2406 });
2407 self
2408 }
2409
2410 fn rounded_tr(mut self, radius: impl IntoReactive<Length>) -> Self {
2412 self.style().bind_value(radius, |s, radius: Length| {
2413 s.lengths(&[StyleKey::BorderTopRightRadius], radius);
2414 });
2415 self
2416 }
2417
2418 fn rounded_br(mut self, radius: impl IntoReactive<Length>) -> Self {
2420 self.style().bind_value(radius, |s, radius: Length| {
2421 s.lengths(&[StyleKey::BorderBottomRightRadius], radius);
2422 });
2423 self
2424 }
2425
2426 fn rounded_bl(mut self, radius: impl IntoReactive<Length>) -> Self {
2428 self.style().bind_value(radius, |s, radius: Length| {
2429 s.lengths(&[StyleKey::BorderBottomLeftRadius], radius);
2430 });
2431 self
2432 }
2433
2434 fn rounded_l(mut self, radius: impl IntoReactive<Length>) -> Self {
2436 self.style().bind_value(radius, |s, radius: Length| {
2437 s.lengths(
2438 &[
2439 StyleKey::BorderTopLeftRadius,
2440 StyleKey::BorderBottomLeftRadius,
2441 ],
2442 radius,
2443 );
2444 });
2445 self
2446 }
2447
2448 fn rounded_r(mut self, radius: impl IntoReactive<Length>) -> Self {
2450 self.style().bind_value(radius, |s, radius: Length| {
2451 s.lengths(
2452 &[
2453 StyleKey::BorderTopRightRadius,
2454 StyleKey::BorderBottomRightRadius,
2455 ],
2456 radius,
2457 );
2458 });
2459 self
2460 }
2461
2462 fn rounded_ss(mut self, radius: impl IntoReactive<Length>) -> Self {
2465 self.style().bind_value(radius, |s, radius: Length| {
2466 s.lengths(&[StyleKey::BorderTopStartRadius], radius);
2467 });
2468 self
2469 }
2470
2471 fn rounded_se(mut self, radius: impl IntoReactive<Length>) -> Self {
2473 self.style().bind_value(radius, |s, radius: Length| {
2474 s.lengths(&[StyleKey::BorderTopEndRadius], radius);
2475 });
2476 self
2477 }
2478
2479 fn rounded_ee(mut self, radius: impl IntoReactive<Length>) -> Self {
2481 self.style().bind_value(radius, |s, radius: Length| {
2482 s.lengths(&[StyleKey::BorderBottomEndRadius], radius);
2483 });
2484 self
2485 }
2486
2487 fn rounded_es(mut self, radius: impl IntoReactive<Length>) -> Self {
2489 self.style().bind_value(radius, |s, radius: Length| {
2490 s.lengths(&[StyleKey::BorderBottomStartRadius], radius);
2491 });
2492 self
2493 }
2494
2495 fn rounded_s(mut self, radius: impl IntoReactive<Length>) -> Self {
2497 self.style().bind_value(radius, |s, radius: Length| {
2498 s.lengths(
2499 &[
2500 StyleKey::BorderTopStartRadius,
2501 StyleKey::BorderBottomStartRadius,
2502 ],
2503 radius,
2504 );
2505 });
2506 self
2507 }
2508
2509 fn rounded_e(mut self, radius: impl IntoReactive<Length>) -> Self {
2511 self.style().bind_value(radius, |s, radius: Length| {
2512 s.lengths(
2513 &[
2514 StyleKey::BorderTopEndRadius,
2515 StyleKey::BorderBottomEndRadius,
2516 ],
2517 radius,
2518 );
2519 });
2520 self
2521 }
2522
2523 fn outline(mut self, width: impl Into<AbsoluteLength>) -> Self {
2526 let s = self.style();
2527 s.number(StyleKey::OutlineWidth, points(width.into()));
2528 s.push(Declaration::Default(
2529 StyleKey::OutlineStyle,
2530 PropertyValue::BorderStyle(crate::BorderStyle::Solid, false),
2531 ));
2532 self
2533 }
2534
2535 fn outline_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2537 self.style().bind_value(color, |s, color: AdaptiveColor| {
2538 s.color(&[StyleKey::OutlineColor], color);
2539 });
2540 self
2541 }
2542
2543 fn outline_offset(mut self, offset: impl IntoReactive<AbsoluteLength>) -> Self {
2545 self.style()
2546 .bind_value(offset, |s, offset: AbsoluteLength| {
2547 s.number(StyleKey::OutlineOffset, points(offset));
2548 });
2549 self
2550 }
2551
2552 fn outline_dashed(mut self) -> Self {
2555 let s = self.style();
2556 s.value(
2557 StyleKey::OutlineStyle,
2558 PropertyValue::BorderStyle(crate::BorderStyle::Dashed, false),
2559 );
2560 s.push(Declaration::Default(
2561 StyleKey::OutlineWidth,
2562 PropertyValue::Points(px(3.0)),
2563 ));
2564 self
2565 }
2566
2567 fn outline_dotted(mut self) -> Self {
2570 let s = self.style();
2571 s.value(
2572 StyleKey::OutlineStyle,
2573 PropertyValue::BorderStyle(crate::BorderStyle::Dotted, false),
2574 );
2575 s.push(Declaration::Default(
2576 StyleKey::OutlineWidth,
2577 PropertyValue::Points(px(3.0)),
2578 ));
2579 self
2580 }
2581
2582 fn outline_none(mut self) -> Self {
2584 self.style().value(
2585 StyleKey::OutlineStyle,
2586 PropertyValue::BorderStyle(crate::BorderStyle::Solid, true),
2587 );
2588 self
2589 }
2590
2591 fn shadow_none(mut self) -> Self {
2593 self.style().push(Declaration::Shadow("none"));
2594 self
2595 }
2596
2597 fn shadow_2xs(mut self) -> Self {
2599 self.style().push(Declaration::Shadow("2xs"));
2600 self
2601 }
2602
2603 fn shadow_xs(mut self) -> Self {
2605 self.style().push(Declaration::Shadow("xs"));
2606 self
2607 }
2608
2609 fn shadow_sm(mut self) -> Self {
2611 self.style().push(Declaration::Shadow("sm"));
2612 self
2613 }
2614
2615 fn shadow_md(mut self) -> Self {
2617 self.style().push(Declaration::Shadow("md"));
2618 self
2619 }
2620
2621 fn shadow_lg(mut self) -> Self {
2623 self.style().push(Declaration::Shadow("lg"));
2624 self
2625 }
2626
2627 fn shadow_xl(mut self) -> Self {
2629 self.style().push(Declaration::Shadow("xl"));
2630 self
2631 }
2632
2633 fn shadow_2xl(mut self) -> Self {
2635 self.style().push(Declaration::Shadow("2xl"));
2636 self
2637 }
2638
2639 fn shadow(mut self, layers: impl IntoIterator<Item = BoxShadow>) -> Self {
2642 let layers: Arc<[BoxShadow]> = layers.into_iter().collect();
2643 self.style().push(Declaration::Shadows(Layers(layers)));
2644 self
2645 }
2646
2647 fn shadow_color(mut self, color: impl Into<AdaptiveColor>) -> Self {
2651 let color = color.into();
2652 self.style()
2653 .push(Declaration::PartColor(ColorPart::Shadow, color));
2654 self
2655 }
2656
2657 fn inset_shadow_2xs(mut self) -> Self {
2659 self.style().push(Declaration::InsetShadow("2xs"));
2660 self
2661 }
2662
2663 fn inset_shadow_xs(mut self) -> Self {
2665 self.style().push(Declaration::InsetShadow("xs"));
2666 self
2667 }
2668
2669 fn inset_shadow_sm(mut self) -> Self {
2671 self.style().push(Declaration::InsetShadow("sm"));
2672 self
2673 }
2674
2675 fn inset_shadow_none(mut self) -> Self {
2677 self.style().push(Declaration::InsetShadow("none"));
2678 self
2679 }
2680
2681 fn inset_shadow_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2684 self.style().bind_value(color, |s, color: AdaptiveColor| {
2685 s.push(Declaration::PartColor(ColorPart::InsetShadow, color));
2686 });
2687 self
2688 }
2689
2690 fn ring(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2693 self.style().bind_value(width, |s, width: AbsoluteLength| {
2694 s.push(Declaration::Ring(Bits(points(width))));
2695 });
2696 self
2697 }
2698
2699 fn ring_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2701 self.style().bind_value(color, |s, color: AdaptiveColor| {
2702 s.push(Declaration::PartColor(ColorPart::Ring, color));
2703 });
2704 self
2705 }
2706
2707 fn ring_offset(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2709 self.style().bind_value(width, |s, width: AbsoluteLength| {
2710 s.push(Declaration::RingOffset(Bits(points(width))));
2711 });
2712 self
2713 }
2714
2715 fn ring_offset_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2717 self.style().bind_value(color, |s, color: AdaptiveColor| {
2718 s.push(Declaration::PartColor(ColorPart::RingOffset, color));
2719 });
2720 self
2721 }
2722
2723 fn ring_inset(mut self) -> Self {
2725 self.style().push(Declaration::RingInset);
2726 self
2727 }
2728
2729 fn inset_ring(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
2731 self.style().bind_value(width, |s, width: AbsoluteLength| {
2732 s.push(Declaration::InsetRing(Bits(points(width))));
2733 });
2734 self
2735 }
2736
2737 fn inset_ring_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2739 self.style().bind_value(color, |s, color: AdaptiveColor| {
2740 s.push(Declaration::PartColor(ColorPart::InsetRing, color));
2741 });
2742 self
2743 }
2744
2745 fn bg_linear(mut self, angle: impl IntoReactive<Radians>) -> Self {
2749 self.style().bind_value(angle, |s, angle: Radians| {
2750 s.push(Declaration::Gradient(GradientShape::Linear(Bits(angle.0))));
2751 });
2752 self
2753 }
2754
2755 fn bg_linear_to_t(self) -> Self {
2757 self.bg_linear(deg(0.0))
2758 }
2759
2760 fn bg_linear_to_r(self) -> Self {
2762 self.bg_linear(deg(90.0))
2763 }
2764
2765 fn bg_linear_to_b(self) -> Self {
2767 self.bg_linear(deg(180.0))
2768 }
2769
2770 fn bg_linear_to_l(self) -> Self {
2772 self.bg_linear(deg(270.0))
2773 }
2774
2775 fn bg_linear_to_tr(mut self) -> Self {
2777 self.style()
2778 .push(Declaration::Gradient(GradientShape::Corner {
2779 right: true,
2780 bottom: false,
2781 }));
2782 self
2783 }
2784
2785 fn bg_linear_to_br(mut self) -> Self {
2787 self.style()
2788 .push(Declaration::Gradient(GradientShape::Corner {
2789 right: true,
2790 bottom: true,
2791 }));
2792 self
2793 }
2794
2795 fn bg_linear_to_bl(mut self) -> Self {
2797 self.style()
2798 .push(Declaration::Gradient(GradientShape::Corner {
2799 right: false,
2800 bottom: true,
2801 }));
2802 self
2803 }
2804
2805 fn bg_linear_to_tl(mut self) -> Self {
2807 self.style()
2808 .push(Declaration::Gradient(GradientShape::Corner {
2809 right: false,
2810 bottom: false,
2811 }));
2812 self
2813 }
2814
2815 fn bg_radial(mut self) -> Self {
2817 self.style()
2818 .push(Declaration::Gradient(GradientShape::Radial));
2819 self
2820 }
2821
2822 fn bg_conic(mut self, angle: impl IntoReactive<Radians>) -> Self {
2824 self.style().bind_value(angle, |s, angle: Radians| {
2825 s.push(Declaration::Gradient(GradientShape::Conic(Bits(angle.0))));
2826 });
2827 self
2828 }
2829
2830 fn bg_gradient_none(mut self) -> Self {
2832 self.style().push(Declaration::GradientNone);
2833 self
2834 }
2835
2836 fn gradient_from(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2838 self.style().bind_value(color, |s, color: AdaptiveColor| {
2839 s.push(Declaration::PartColor(ColorPart::From, color));
2840 });
2841 self
2842 }
2843
2844 fn gradient_via(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2846 self.style().bind_value(color, |s, color: AdaptiveColor| {
2847 s.push(Declaration::PartColor(ColorPart::Via, color));
2848 });
2849 self
2850 }
2851
2852 fn gradient_to(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
2854 self.style().bind_value(color, |s, color: AdaptiveColor| {
2855 s.push(Declaration::PartColor(ColorPart::To, color));
2856 });
2857 self
2858 }
2859
2860 fn gradient_from_position(mut self, at: impl IntoReactive<Percentage>) -> Self {
2862 self.style().bind_value(at, |s, at: Percentage| {
2863 s.push(Declaration::StopPosition(0, Bits(at.0)));
2864 });
2865 self
2866 }
2867
2868 fn gradient_via_position(mut self, at: impl IntoReactive<Percentage>) -> Self {
2870 self.style().bind_value(at, |s, at: Percentage| {
2871 s.push(Declaration::StopPosition(1, Bits(at.0)));
2872 });
2873 self
2874 }
2875
2876 fn gradient_to_position(mut self, at: impl IntoReactive<Percentage>) -> Self {
2878 self.style().bind_value(at, |s, at: Percentage| {
2879 s.push(Declaration::StopPosition(2, Bits(at.0)));
2880 });
2881 self
2882 }
2883
2884 fn blur(self, radius: impl Into<Points>) -> Self {
2887 filter(self, false, FilterFn::Blur, radius.into().0)
2888 }
2889
2890 fn brightness(self, amount: f32) -> Self {
2892 filter(self, false, FilterFn::Brightness, amount)
2893 }
2894
2895 fn contrast(self, amount: f32) -> Self {
2897 filter(self, false, FilterFn::Contrast, amount)
2898 }
2899
2900 fn grayscale(self, amount: f32) -> Self {
2902 filter(self, false, FilterFn::Grayscale, amount)
2903 }
2904
2905 fn hue_rotate(self, angle: impl Into<Radians>) -> Self {
2907 filter(self, false, FilterFn::HueRotate, angle.into().to_degrees())
2908 }
2909
2910 fn invert(self, amount: f32) -> Self {
2912 filter(self, false, FilterFn::Invert, amount)
2913 }
2914
2915 fn saturate(self, amount: f32) -> Self {
2917 filter(self, false, FilterFn::Saturate, amount)
2918 }
2919
2920 fn sepia(self, amount: f32) -> Self {
2922 filter(self, false, FilterFn::Sepia, amount)
2923 }
2924
2925 fn filter_none(mut self) -> Self {
2927 self.style().push(Declaration::FilterNone(false));
2928 self
2929 }
2930
2931 fn backdrop_blur(self, radius: impl Into<Points>) -> Self {
2934 filter(self, true, FilterFn::Blur, radius.into().0)
2935 }
2936
2937 fn backdrop_brightness(self, amount: f32) -> Self {
2939 filter(self, true, FilterFn::Brightness, amount)
2940 }
2941
2942 fn backdrop_contrast(self, amount: f32) -> Self {
2944 filter(self, true, FilterFn::Contrast, amount)
2945 }
2946
2947 fn backdrop_grayscale(self, amount: f32) -> Self {
2949 filter(self, true, FilterFn::Grayscale, amount)
2950 }
2951
2952 fn backdrop_hue_rotate(self, angle: impl Into<Radians>) -> Self {
2954 filter(self, true, FilterFn::HueRotate, angle.into().to_degrees())
2955 }
2956
2957 fn backdrop_invert(self, amount: f32) -> Self {
2959 filter(self, true, FilterFn::Invert, amount)
2960 }
2961
2962 fn backdrop_opacity(self, amount: f32) -> Self {
2964 filter(self, true, FilterFn::Opacity, amount)
2965 }
2966
2967 fn backdrop_saturate(self, amount: f32) -> Self {
2969 filter(self, true, FilterFn::Saturate, amount)
2970 }
2971
2972 fn backdrop_sepia(self, amount: f32) -> Self {
2974 filter(self, true, FilterFn::Sepia, amount)
2975 }
2976
2977 fn backdrop_filter_none(mut self) -> Self {
2979 self.style().push(Declaration::FilterNone(true));
2980 self
2981 }
2982
2983 fn mix_blend_mode(mut self, mode: BlendMode) -> Self {
2985 self.style()
2986 .value(StyleKey::MixBlendMode, PropertyValue::BlendMode(mode));
2987 self
2988 }
2989
2990 fn pointer_events(mut self, pointer_events: impl IntoReactive<crate::PointerEvents>) -> Self {
2994 self.style()
2995 .bind_value(pointer_events, |s, p: crate::PointerEvents| {
2996 s.value(
2997 StyleKey::PointerEvents,
2998 PropertyValue::PointerEvents(Some(p)),
2999 );
3000 });
3001 self
3002 }
3003
3004 fn select_text(mut self) -> Self {
3007 self.style()
3008 .value(StyleKey::UserSelect, PropertyValue::OptBool(Some(true)));
3009 self
3010 }
3011
3012 fn select_none(mut self) -> Self {
3015 self.style()
3016 .value(StyleKey::UserSelect, PropertyValue::OptBool(Some(false)));
3017 self
3018 }
3019
3020 fn glass_effect(mut self, glass: crate::GlassEffect) -> Self {
3023 self.style()
3024 .value(StyleKey::GlassEffect, PropertyValue::GlassEffect(glass));
3025 self
3026 }
3027
3028 fn glass_tint_color(mut self, color: impl IntoReactive<Option<AdaptiveColor>>) -> Self {
3033 self.style()
3034 .bind_value(color, |s, color: Option<AdaptiveColor>| match color {
3035 Some(color) => s.color(&[StyleKey::GlassTintColor], color),
3036 None => s.value(StyleKey::GlassTintColor, PropertyValue::Color(None)),
3037 });
3038 self
3039 }
3040
3041 fn cursor(mut self, cursor: Cursor) -> Self {
3044 self.style()
3045 .value(StyleKey::Cursor, PropertyValue::Cursor(cursor));
3046 self
3047 }
3048
3049 fn cursor_pointer(self) -> Self {
3052 self.cursor(Cursor::Pointer)
3053 }
3054
3055 fn transition(self) -> Self {
3058 transition(
3059 self,
3060 TransitionProps::COLORS
3061 | TransitionProps::OPACITY
3062 | TransitionProps::SHADOW
3063 | TransitionProps::TRANSFORM,
3064 )
3065 }
3066
3067 fn transition_all(self) -> Self {
3069 transition(self, TransitionProps::ALL)
3070 }
3071
3072 fn transition_colors(self) -> Self {
3074 transition(self, TransitionProps::COLORS)
3075 }
3076
3077 fn transition_opacity(self) -> Self {
3079 transition(self, TransitionProps::OPACITY)
3080 }
3081
3082 fn transition_shadow(self) -> Self {
3084 transition(self, TransitionProps::SHADOW)
3085 }
3086
3087 fn transition_transform(self) -> Self {
3089 transition(self, TransitionProps::TRANSFORM)
3090 }
3091
3092 fn transition_none(mut self) -> Self {
3094 self.style().push(Declaration::TransitionNone);
3095 self
3096 }
3097
3098 fn duration(mut self, ms: impl IntoReactive<f32>) -> Self {
3100 self.style().bind_value(ms, |s, ms: f32| {
3101 s.push(Declaration::Duration(Bits(ms)));
3102 });
3103 self
3104 }
3105
3106 fn delay(mut self, ms: impl IntoReactive<f32>) -> Self {
3108 self.style().bind_value(ms, |s, ms: f32| {
3109 s.push(Declaration::Delay(Bits(ms)));
3110 });
3111 self
3112 }
3113
3114 fn ease_linear(mut self) -> Self {
3116 self.style().push(Declaration::EasePreset("linear"));
3117 self
3118 }
3119
3120 fn ease_in(mut self) -> Self {
3122 self.style().push(Declaration::EasePreset("in"));
3123 self
3124 }
3125
3126 fn ease_out(mut self) -> Self {
3128 self.style().push(Declaration::EasePreset("out"));
3129 self
3130 }
3131
3132 fn ease_in_out(mut self) -> Self {
3134 self.style().push(Declaration::EasePreset("in-out"));
3135 self
3136 }
3137
3138 fn ease_bezier(mut self, x1: f32, y1: f32, x2: f32, y2: f32) -> Self {
3140 self.style()
3141 .push(Declaration::Ease([x1, y1, x2, y2].map(Bits)));
3142 self
3143 }
3144
3145 fn animate_spin(mut self) -> Self {
3147 self.style().push(Declaration::Animate(Some(0)));
3148 self
3149 }
3150
3151 fn animate_ping(mut self) -> Self {
3153 self.style().push(Declaration::Animate(Some(1)));
3154 self
3155 }
3156
3157 fn animate_pulse(mut self) -> Self {
3159 self.style().push(Declaration::Animate(Some(2)));
3160 self
3161 }
3162
3163 fn animate_bounce(mut self) -> Self {
3165 self.style().push(Declaration::Animate(Some(3)));
3166 self
3167 }
3168
3169 fn animation(mut self, animation: KeyframeAnimation) -> Self {
3175 let index = KEYFRAMES
3176 .iter()
3177 .position(|k| *k == animation.keyframes)
3178 .expect("every keyframes") as u8;
3179 self.style().push(Declaration::Animation(
3180 index,
3181 Bits(animation.duration),
3182 Bits(animation.delay),
3183 animation.easing.map(Bits),
3184 animation.iterations,
3185 crate::style::frame_rate(animation.frame_rate).map(Bits),
3186 ));
3187 self
3188 }
3189
3190 fn animate_none(mut self) -> Self {
3192 self.style().push(Declaration::Animate(None));
3193 self
3194 }
3195
3196 fn scale(self, scale: impl IntoReactive<f32>) -> Self {
3198 self.transform_step(TransformKind::Scale, scale)
3199 }
3200
3201 fn scale_x(self, scale: impl IntoReactive<f32>) -> Self {
3203 self.transform_step(TransformKind::ScaleX, scale)
3204 }
3205
3206 fn scale_y(self, scale: impl IntoReactive<f32>) -> Self {
3208 self.transform_step(TransformKind::ScaleY, scale)
3209 }
3210
3211 fn translate_x(self, offset: impl IntoReactive<DefiniteLength>) -> Self {
3214 transform_with(self, offset, |offset| {
3215 translation(
3216 offset,
3217 TransformKind::TranslateX,
3218 TransformKind::TranslateXFraction,
3219 )
3220 })
3221 }
3222
3223 fn translate_y(self, offset: impl IntoReactive<DefiniteLength>) -> Self {
3226 transform_with(self, offset, |offset| {
3227 translation(
3228 offset,
3229 TransformKind::TranslateY,
3230 TransformKind::TranslateYFraction,
3231 )
3232 })
3233 }
3234
3235 fn rotate(self, angle: impl IntoReactive<Radians>) -> Self {
3237 transform_with(self, angle, |a: Radians| {
3238 (TransformKind::Rotate, a.to_degrees())
3239 })
3240 }
3241
3242 fn rotate_x(self, angle: impl IntoReactive<Radians>) -> Self {
3245 transform_with(self, angle, |a: Radians| {
3246 (TransformKind::RotateX, a.to_degrees())
3247 })
3248 }
3249
3250 fn rotate_y(self, angle: impl IntoReactive<Radians>) -> Self {
3252 transform_with(self, angle, |a: Radians| {
3253 (TransformKind::RotateY, a.to_degrees())
3254 })
3255 }
3256
3257 fn skew_x(self, angle: impl IntoReactive<Radians>) -> Self {
3259 transform_with(self, angle, |a: Radians| {
3260 (TransformKind::SkewX, a.to_degrees())
3261 })
3262 }
3263
3264 fn skew_y(self, angle: impl IntoReactive<Radians>) -> Self {
3266 transform_with(self, angle, |a: Radians| {
3267 (TransformKind::SkewY, a.to_degrees())
3268 })
3269 }
3270
3271 fn perspective(self, distance: impl IntoReactive<AbsoluteLength>) -> Self {
3275 transform_with(self, distance, |d: AbsoluteLength| {
3276 (TransformKind::Perspective, points(d))
3277 })
3278 }
3279
3280 fn transform_step(self, kind: TransformKind, amount: impl IntoReactive<f32>) -> Self {
3286 transform_with(self, amount, move |amount| (kind, amount))
3287 }
3288
3289 fn transform_none(self) -> Self {
3294 transform_reset(self, Declaration::TransformNone)
3295 }
3296
3297 fn translate_none(self) -> Self {
3299 transform_reset(self, Declaration::TranslateNone)
3300 }
3301
3302 fn scale_none(self) -> Self {
3304 transform_reset(self, Declaration::ScaleNone)
3305 }
3306
3307 fn rotate_none(self) -> Self {
3309 transform_reset(self, Declaration::RotateNone)
3310 }
3311
3312 fn transform_origin(mut self, origin: TransformOrigin) -> Self {
3315 let axis = |l: DefiniteLength| match l {
3316 DefiniteLength::Absolute(a) => (false, Bits(a.to_points(REM_SIZE).0)),
3317 DefiniteLength::Fraction(f) => (true, Bits(f.0)),
3318 };
3319 self.style()
3320 .push(Declaration::Origin([axis(origin.x), axis(origin.y)]));
3321 self
3322 }
3323
3324 fn origin_top_left(mut self) -> Self {
3326 self.style().value(
3327 StyleKey::TransformOrigin,
3328 PropertyValue::TransformOrigin(origin(0.0, 0.0)),
3329 );
3330 self
3331 }
3332
3333 fn origin_top(mut self) -> Self {
3335 self.style().value(
3336 StyleKey::TransformOrigin,
3337 PropertyValue::TransformOrigin(origin(0.5, 0.0)),
3338 );
3339 self
3340 }
3341
3342 fn origin_top_right(mut self) -> Self {
3344 self.style().value(
3345 StyleKey::TransformOrigin,
3346 PropertyValue::TransformOrigin(origin(1.0, 0.0)),
3347 );
3348 self
3349 }
3350
3351 fn origin_left(mut self) -> Self {
3353 self.style().value(
3354 StyleKey::TransformOrigin,
3355 PropertyValue::TransformOrigin(origin(0.0, 0.5)),
3356 );
3357 self
3358 }
3359
3360 fn origin_center(mut self) -> Self {
3362 self.style().value(
3363 StyleKey::TransformOrigin,
3364 PropertyValue::TransformOrigin(origin(0.5, 0.5)),
3365 );
3366 self
3367 }
3368
3369 fn origin_right(mut self) -> Self {
3371 self.style().value(
3372 StyleKey::TransformOrigin,
3373 PropertyValue::TransformOrigin(origin(1.0, 0.5)),
3374 );
3375 self
3376 }
3377
3378 fn origin_bottom_left(mut self) -> Self {
3380 self.style().value(
3381 StyleKey::TransformOrigin,
3382 PropertyValue::TransformOrigin(origin(0.0, 1.0)),
3383 );
3384 self
3385 }
3386
3387 fn origin_bottom(mut self) -> Self {
3389 self.style().value(
3390 StyleKey::TransformOrigin,
3391 PropertyValue::TransformOrigin(origin(0.5, 1.0)),
3392 );
3393 self
3394 }
3395
3396 fn origin_bottom_right(mut self) -> Self {
3398 self.style().value(
3399 StyleKey::TransformOrigin,
3400 PropertyValue::TransformOrigin(origin(1.0, 1.0)),
3401 );
3402 self
3403 }
3404
3405 fn object_fit(mut self, fit: impl IntoReactive<crate::ResizeMode>) -> Self {
3409 self.style().bind_value(fit, |s, fit: crate::ResizeMode| {
3410 s.value(StyleKey::ObjectFit, PropertyValue::ObjectFit(Some(fit)));
3411 });
3412 self
3413 }
3414
3415 fn object_position(mut self, position: ObjectPosition) -> Self {
3419 self.style().value(
3420 StyleKey::ObjectPosition,
3421 PropertyValue::ObjectPosition(position),
3422 );
3423 self
3424 }
3425
3426 fn object_top_left(mut self) -> Self {
3428 self.style().value(
3429 StyleKey::ObjectPosition,
3430 PropertyValue::ObjectPosition(ObjectPosition::new(
3431 PositionAxis::START,
3432 PositionAxis::START,
3433 )),
3434 );
3435 self
3436 }
3437
3438 fn object_top(mut self) -> Self {
3441 self.style().value(
3442 StyleKey::ObjectPosition,
3443 PropertyValue::ObjectPosition(ObjectPosition::new(
3444 PositionAxis::CENTER,
3445 PositionAxis::START,
3446 )),
3447 );
3448 self
3449 }
3450
3451 fn object_top_right(mut self) -> Self {
3453 self.style().value(
3454 StyleKey::ObjectPosition,
3455 PropertyValue::ObjectPosition(ObjectPosition::new(
3456 PositionAxis::END,
3457 PositionAxis::START,
3458 )),
3459 );
3460 self
3461 }
3462
3463 fn object_left(mut self) -> Self {
3465 self.style().value(
3466 StyleKey::ObjectPosition,
3467 PropertyValue::ObjectPosition(ObjectPosition::new(
3468 PositionAxis::START,
3469 PositionAxis::CENTER,
3470 )),
3471 );
3472 self
3473 }
3474
3475 fn object_center(mut self) -> Self {
3478 self.style().value(
3479 StyleKey::ObjectPosition,
3480 PropertyValue::ObjectPosition(ObjectPosition::new(
3481 PositionAxis::CENTER,
3482 PositionAxis::CENTER,
3483 )),
3484 );
3485 self
3486 }
3487
3488 fn object_right(mut self) -> Self {
3490 self.style().value(
3491 StyleKey::ObjectPosition,
3492 PropertyValue::ObjectPosition(ObjectPosition::new(
3493 PositionAxis::END,
3494 PositionAxis::CENTER,
3495 )),
3496 );
3497 self
3498 }
3499
3500 fn object_bottom_left(mut self) -> Self {
3503 self.style().value(
3504 StyleKey::ObjectPosition,
3505 PropertyValue::ObjectPosition(ObjectPosition::new(
3506 PositionAxis::START,
3507 PositionAxis::END,
3508 )),
3509 );
3510 self
3511 }
3512
3513 fn object_bottom(mut self) -> Self {
3515 self.style().value(
3516 StyleKey::ObjectPosition,
3517 PropertyValue::ObjectPosition(ObjectPosition::new(
3518 PositionAxis::CENTER,
3519 PositionAxis::END,
3520 )),
3521 );
3522 self
3523 }
3524
3525 fn object_bottom_right(mut self) -> Self {
3528 self.style().value(
3529 StyleKey::ObjectPosition,
3530 PropertyValue::ObjectPosition(ObjectPosition::new(
3531 PositionAxis::END,
3532 PositionAxis::END,
3533 )),
3534 );
3535 self
3536 }
3537
3538 fn backface_hidden(mut self) -> Self {
3540 self.style()
3541 .value(StyleKey::BackfaceVisibility, PropertyValue::Bool(true));
3542 self
3543 }
3544
3545 fn backface_visible(mut self) -> Self {
3547 self.style()
3548 .value(StyleKey::BackfaceVisibility, PropertyValue::Bool(false));
3549 self
3550 }
3551
3552 fn text_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
3556 self.style().bind_value(color, |s, color: AdaptiveColor| {
3557 s.color(&[StyleKey::Color], color);
3558 });
3559 self
3560 }
3561
3562 fn tint_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
3565 self.style().bind_value(color, |s, color: AdaptiveColor| {
3566 s.color(&[StyleKey::TintColor], color);
3567 });
3568 self
3569 }
3570
3571 fn fill_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
3575 self.style().bind_value(color, |s, color: AdaptiveColor| {
3576 s.color(&[StyleKey::Fill], color);
3577 });
3578 self
3579 }
3580
3581 fn fill_none(mut self) -> Self {
3583 self.style().value(
3584 StyleKey::Fill,
3585 PropertyValue::Paint(Some(crate::SvgPaint::None)),
3586 );
3587 self
3588 }
3589
3590 fn fill_current(mut self) -> Self {
3592 self.style().value(
3593 StyleKey::Fill,
3594 PropertyValue::Paint(Some(crate::SvgPaint::CurrentColor)),
3595 );
3596 self
3597 }
3598
3599 fn stroke_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
3603 self.style().bind_value(color, |s, color: AdaptiveColor| {
3604 s.color(&[StyleKey::Stroke], color);
3605 });
3606 self
3607 }
3608
3609 fn stroke_none(mut self) -> Self {
3611 self.style().value(
3612 StyleKey::Stroke,
3613 PropertyValue::Paint(Some(crate::SvgPaint::None)),
3614 );
3615 self
3616 }
3617
3618 fn stroke_current(mut self) -> Self {
3620 self.style().value(
3621 StyleKey::Stroke,
3622 PropertyValue::Paint(Some(crate::SvgPaint::CurrentColor)),
3623 );
3624 self
3625 }
3626
3627 fn stroke_width(mut self, width: impl IntoReactive<AbsoluteLength>) -> Self {
3630 self.style().bind_value(width, |s, width: AbsoluteLength| {
3631 s.number(StyleKey::StrokeWidth, points(width));
3632 });
3633 self
3634 }
3635
3636 fn text_size(mut self, size: impl IntoReactive<Length>) -> Self {
3638 self.style().bind_value(size, |s, size: Length| {
3639 s.lengths(&[StyleKey::FontSize], size);
3640 });
3641 self
3642 }
3643
3644 fn text_xs(mut self) -> Self {
3646 self.style().push(Declaration::TextPreset("xs"));
3647 self
3648 }
3649
3650 fn text_sm(mut self) -> Self {
3652 self.style().push(Declaration::TextPreset("sm"));
3653 self
3654 }
3655
3656 fn text_base(mut self) -> Self {
3658 self.style().push(Declaration::TextPreset("base"));
3659 self
3660 }
3661
3662 fn text_lg(mut self) -> Self {
3664 self.style().push(Declaration::TextPreset("lg"));
3665 self
3666 }
3667
3668 fn text_xl(mut self) -> Self {
3670 self.style().push(Declaration::TextPreset("xl"));
3671 self
3672 }
3673
3674 fn text_2xl(mut self) -> Self {
3676 self.style().push(Declaration::TextPreset("2xl"));
3677 self
3678 }
3679
3680 fn text_3xl(mut self) -> Self {
3682 self.style().push(Declaration::TextPreset("3xl"));
3683 self
3684 }
3685
3686 fn text_4xl(mut self) -> Self {
3688 self.style().push(Declaration::TextPreset("4xl"));
3689 self
3690 }
3691
3692 fn text_5xl(mut self) -> Self {
3694 self.style().push(Declaration::TextPreset("5xl"));
3695 self
3696 }
3697
3698 fn text_6xl(mut self) -> Self {
3700 self.style().push(Declaration::TextPreset("6xl"));
3701 self
3702 }
3703
3704 fn text_7xl(mut self) -> Self {
3706 self.style().push(Declaration::TextPreset("7xl"));
3707 self
3708 }
3709
3710 fn text_8xl(mut self) -> Self {
3712 self.style().push(Declaration::TextPreset("8xl"));
3713 self
3714 }
3715
3716 fn text_9xl(mut self) -> Self {
3718 self.style().push(Declaration::TextPreset("9xl"));
3719 self
3720 }
3721
3722 fn line_height(mut self, height: impl IntoReactive<LineHeight>) -> Self {
3725 self.style().bind_value(height, |s, height: LineHeight| {
3726 s.value(StyleKey::LineHeight, PropertyValue::LineHeight(height));
3727 });
3728 self
3729 }
3730
3731 fn font_weight(mut self, weight: FontWeight) -> Self {
3733 self.style().number(StyleKey::FontWeight, weight.0 as f32);
3734 self
3735 }
3736
3737 fn font_family(mut self, family: impl IntoReactive<SharedString>) -> Self {
3739 self.style().bind_value(family, |s, family: SharedString| {
3740 s.value(
3741 StyleKey::FontFamily,
3742 PropertyValue::FontFamily(Some(crate::FontFamily::new(family.trim()))),
3743 );
3744 });
3745 self
3746 }
3747
3748 fn italic(mut self) -> Self {
3750 self.style()
3751 .value(StyleKey::FontStyle, PropertyValue::OptBool(Some(true)));
3752 self
3753 }
3754
3755 fn underline(mut self) -> Self {
3757 self.style().push(Declaration::Decoration(UNDERLINE));
3758 self
3759 }
3760
3761 fn line_through(mut self) -> Self {
3763 self.style().push(Declaration::Decoration(LINE_THROUGH));
3764 self
3765 }
3766
3767 fn no_underline(mut self) -> Self {
3769 self.style().push(Declaration::Decoration(0));
3770 self
3771 }
3772
3773 fn text_decoration_color(mut self, color: impl IntoReactive<AdaptiveColor>) -> Self {
3776 self.style().bind_value(color, |s, color: AdaptiveColor| {
3777 s.color(&[StyleKey::TextDecorationColor], color);
3778 });
3779 self
3780 }
3781
3782 fn text_decoration_solid(mut self) -> Self {
3784 self.style().value(
3785 StyleKey::TextDecorationStyle,
3786 PropertyValue::DecorationStyle(Some(crate::DecorationStyle::Solid)),
3787 );
3788 self
3789 }
3790
3791 fn text_decoration_double(mut self) -> Self {
3793 self.style().value(
3794 StyleKey::TextDecorationStyle,
3795 PropertyValue::DecorationStyle(Some(crate::DecorationStyle::Double)),
3796 );
3797 self
3798 }
3799
3800 fn text_decoration_dotted(mut self) -> Self {
3802 self.style().value(
3803 StyleKey::TextDecorationStyle,
3804 PropertyValue::DecorationStyle(Some(crate::DecorationStyle::Dotted)),
3805 );
3806 self
3807 }
3808
3809 fn text_decoration_dashed(mut self) -> Self {
3811 self.style().value(
3812 StyleKey::TextDecorationStyle,
3813 PropertyValue::DecorationStyle(Some(crate::DecorationStyle::Dashed)),
3814 );
3815 self
3816 }
3817
3818 fn text_decoration_wavy(mut self) -> Self {
3820 self.style().value(
3821 StyleKey::TextDecorationStyle,
3822 PropertyValue::DecorationStyle(Some(crate::DecorationStyle::Wavy)),
3823 );
3824 self
3825 }
3826
3827 fn text_decoration_thickness(mut self, thickness: impl IntoReactive<Points>) -> Self {
3829 self.style().bind_value(thickness, |s, thickness: Points| {
3830 s.number(StyleKey::TextDecorationThickness, thickness.0);
3831 });
3832 self
3833 }
3834
3835 fn underline_offset(mut self, offset: impl IntoReactive<Points>) -> Self {
3837 self.style().bind_value(offset, |s, offset: Points| {
3838 s.number(StyleKey::TextUnderlineOffset, offset.0);
3839 });
3840 self
3841 }
3842
3843 fn uppercase(mut self) -> Self {
3845 self.style().value(
3846 StyleKey::TextTransform,
3847 PropertyValue::TextTransform(Some(crate::TextTransform::Uppercase)),
3848 );
3849 self
3850 }
3851
3852 fn lowercase(mut self) -> Self {
3854 self.style().value(
3855 StyleKey::TextTransform,
3856 PropertyValue::TextTransform(Some(crate::TextTransform::Lowercase)),
3857 );
3858 self
3859 }
3860
3861 fn capitalize(mut self) -> Self {
3863 self.style().value(
3864 StyleKey::TextTransform,
3865 PropertyValue::TextTransform(Some(crate::TextTransform::Capitalize)),
3866 );
3867 self
3868 }
3869
3870 fn normal_case(mut self) -> Self {
3872 self.style().value(
3873 StyleKey::TextTransform,
3874 PropertyValue::TextTransform(Some(crate::TextTransform::None)),
3875 );
3876 self
3877 }
3878
3879 fn tabular_nums(self) -> Self {
3881 font_variant(
3882 self,
3883 FontVariant::TABULAR_NUMS,
3884 FontVariant::PROPORTIONAL_NUMS,
3885 )
3886 }
3887
3888 fn proportional_nums(self) -> Self {
3890 font_variant(
3891 self,
3892 FontVariant::PROPORTIONAL_NUMS,
3893 FontVariant::TABULAR_NUMS,
3894 )
3895 }
3896
3897 fn oldstyle_nums(self) -> Self {
3899 font_variant(self, FontVariant::OLDSTYLE_NUMS, FontVariant::LINING_NUMS)
3900 }
3901
3902 fn lining_nums(self) -> Self {
3904 font_variant(self, FontVariant::LINING_NUMS, FontVariant::OLDSTYLE_NUMS)
3905 }
3906
3907 fn small_caps(self) -> Self {
3909 font_variant(self, FontVariant::SMALL_CAPS, 0)
3910 }
3911
3912 fn normal_nums(self) -> Self {
3915 font_variant(self, 0, !FontVariant::SMALL_CAPS)
3916 }
3917
3918 fn direction(mut self, direction: impl IntoReactive<LayoutDirection>) -> Self {
3921 self.style()
3922 .bind_value(direction, |s, direction: LayoutDirection| {
3923 s.value(
3924 StyleKey::Direction,
3925 PropertyValue::OptBool(match direction {
3926 LayoutDirection::Inherit => None,
3927 LayoutDirection::Ltr => Some(false),
3928 LayoutDirection::Rtl => Some(true),
3929 }),
3930 );
3931 });
3932 self
3933 }
3934
3935 fn number_of_lines(mut self, lines: impl IntoReactive<u32>) -> Self {
3938 self.style().bind_value(lines, |s, lines: u32| {
3939 s.value(
3940 StyleKey::NumberOfLines,
3941 PropertyValue::NumberOfLines(Some(lines)),
3942 );
3943 });
3944 self
3945 }
3946
3947 fn text_overflow(mut self, mode: impl IntoReactive<crate::EllipsizeMode>) -> Self {
3950 self.style()
3951 .bind_value(mode, |s, mode: crate::EllipsizeMode| {
3952 s.value(
3953 StyleKey::TextOverflow,
3954 PropertyValue::TextOverflow(Some(mode)),
3955 );
3956 });
3957 self
3958 }
3959
3960 fn truncate(self) -> Self {
3962 self.number_of_lines(1u32)
3963 .text_overflow(crate::EllipsizeMode::Tail)
3964 }
3965
3966 fn writing_direction(mut self, direction: WritingDirection) -> Self {
3968 self.style().value(
3969 StyleKey::WritingDirection,
3970 PropertyValue::OptBool(match direction {
3971 WritingDirection::Auto => None,
3972 WritingDirection::Ltr => Some(false),
3973 WritingDirection::Rtl => Some(true),
3974 }),
3975 );
3976 self
3977 }
3978
3979 fn text_justify(mut self) -> Self {
3981 self.style().value(
3982 StyleKey::TextAlign,
3983 PropertyValue::TextAlign(Some(crate::TextAlign::Justify), false),
3984 );
3985 self
3986 }
3987
3988 fn text_shadow_2xs(mut self) -> Self {
3990 self.style().push(Declaration::TextShadow("2xs"));
3991 self
3992 }
3993
3994 fn text_shadow_xs(mut self) -> Self {
3996 self.style().push(Declaration::TextShadow("xs"));
3997 self
3998 }
3999
4000 fn text_shadow_sm(mut self) -> Self {
4002 self.style().push(Declaration::TextShadow("sm"));
4003 self
4004 }
4005
4006 fn text_shadow_md(mut self) -> Self {
4008 self.style().push(Declaration::TextShadow("md"));
4009 self
4010 }
4011
4012 fn text_shadow_lg(mut self) -> Self {
4014 self.style().push(Declaration::TextShadow("lg"));
4015 self
4016 }
4017
4018 fn text_shadow_none(mut self) -> Self {
4020 self.style().push(Declaration::TextShadow("none"));
4021 self
4022 }
4023
4024 fn text_shadow_color(mut self, color: impl Into<AdaptiveColor>) -> Self {
4027 let color = color.into();
4028 self.style()
4029 .push(Declaration::PartColor(ColorPart::TextShadow, color));
4030 self
4031 }
4032
4033 fn text_shadow(mut self, layers: impl IntoIterator<Item = crate::TextShadow>) -> Self {
4036 let layers: Arc<[crate::TextShadow]> = layers.into_iter().collect();
4037 self.style()
4038 .push(Declaration::TextShadows(TextLayers(layers)));
4039 self
4040 }
4041
4042 fn text_left(mut self) -> Self {
4044 self.style().value(
4045 StyleKey::TextAlign,
4046 PropertyValue::TextAlign(Some(crate::TextAlign::Left), true),
4047 );
4048 self
4049 }
4050
4051 fn text_center(mut self) -> Self {
4053 self.style().value(
4054 StyleKey::TextAlign,
4055 PropertyValue::TextAlign(Some(crate::TextAlign::Center), false),
4056 );
4057 self
4058 }
4059
4060 fn text_right(mut self) -> Self {
4062 self.style().value(
4063 StyleKey::TextAlign,
4064 PropertyValue::TextAlign(Some(crate::TextAlign::Right), true),
4065 );
4066 self
4067 }
4068
4069 fn align_baseline(mut self) -> Self {
4072 self.style().value(
4073 StyleKey::VerticalAlign,
4074 PropertyValue::VerticalAlign(crate::VerticalAlign::Baseline),
4075 );
4076 self
4077 }
4078
4079 fn align_text_top(mut self) -> Self {
4082 self.style().value(
4083 StyleKey::VerticalAlign,
4084 PropertyValue::VerticalAlign(crate::VerticalAlign::TextTop),
4085 );
4086 self
4087 }
4088
4089 fn align_text_bottom(mut self) -> Self {
4092 self.style().value(
4093 StyleKey::VerticalAlign,
4094 PropertyValue::VerticalAlign(crate::VerticalAlign::TextBottom),
4095 );
4096 self
4097 }
4098
4099 fn align_middle(mut self) -> Self {
4102 self.style().value(
4103 StyleKey::VerticalAlign,
4104 PropertyValue::VerticalAlign(crate::VerticalAlign::Middle),
4105 );
4106 self
4107 }
4108
4109 fn letter_spacing(mut self, spacing: impl IntoReactive<AbsoluteLength>) -> Self {
4111 self.style()
4112 .bind_value(spacing, |s, spacing: AbsoluteLength| {
4113 s.number(StyleKey::LetterSpacing, points(spacing));
4114 });
4115 self
4116 }
4117
4118 fn style_when(
4124 mut self,
4125 when: impl IntoReactive<bool>,
4126 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4127 ) -> Self {
4128 let layer = f(StyleRefinement::default());
4129 match when.into_reactive() {
4130 Reactive::Static(true) => self.style().extend_as(Variant::default(), layer),
4131 Reactive::Static(false) => {}
4132 Reactive::Bound(when) => self.style().bind(move |s| {
4133 if when() {
4134 s.extend_as(Variant::default(), layer.resolved());
4135 }
4136 }),
4137 }
4138 self
4139 }
4140
4141 fn style_with(mut self, f: impl Fn(StyleRefinement) -> StyleRefinement + 'static) -> Self {
4145 self.style().bind(move |s| {
4146 let made = f(StyleRefinement::default()).resolved();
4147 s.extend_as(Variant::default(), made);
4148 });
4149 self
4150 }
4151
4152 fn dark(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4155 let s = f(StyleRefinement::default());
4156 self.style().extend_as(Variant::DARK, s);
4157 self
4158 }
4159
4160 fn selected(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4163 let s = f(StyleRefinement::default());
4164 self.style().extend_as(Variant::SELECTED, s);
4165 self
4166 }
4167
4168 fn checked(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4173 let s = f(StyleRefinement::default());
4174 self.style().extend_as(Variant::CHECKED, s);
4175 self
4176 }
4177
4178 fn indeterminate(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4183 let s = f(StyleRefinement::default());
4184 self.style().extend_as(Variant::INDETERMINATE, s);
4185 self
4186 }
4187
4188 fn expanded(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4192 let s = f(StyleRefinement::default());
4193 self.style().extend_as(Variant::EXPANDED, s);
4194 self
4195 }
4196
4197 fn aria_pressed(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4202 let s = f(StyleRefinement::default());
4203 self.style().extend_as(Variant::ARIA_PRESSED, s);
4204 self
4205 }
4206
4207 fn group(mut self) -> Self {
4210 self.style().push(Declaration::Group(""));
4211 self
4212 }
4213
4214 fn group_named(mut self, name: &'static str) -> Self {
4217 self.style().push(Declaration::Group(name));
4218 self
4219 }
4220
4221 fn group_active(self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4224 in_group(self, "", state::PRESSED, f)
4225 }
4226
4227 fn group_hover(self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4229 in_group(self, "", state::HOVERED, f)
4230 }
4231
4232 fn group_focus_within(self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4235 in_group(self, "", state::FOCUS_WITHIN, f)
4236 }
4237
4238 fn group_focus_visible(self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
4241 in_group(self, "", state::FOCUS_VISIBLE, f)
4242 }
4243
4244 fn group_active_named(
4247 self,
4248 name: &'static str,
4249 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4250 ) -> Self {
4251 in_group(self, name, state::PRESSED, f)
4252 }
4253
4254 fn group_hover_named(
4256 self,
4257 name: &'static str,
4258 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4259 ) -> Self {
4260 in_group(self, name, state::HOVERED, f)
4261 }
4262
4263 fn group_focus_within_named(
4265 self,
4266 name: &'static str,
4267 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4268 ) -> Self {
4269 in_group(self, name, state::FOCUS_WITHIN, f)
4270 }
4271
4272 fn group_focus_visible_named(
4275 self,
4276 name: &'static str,
4277 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4278 ) -> Self {
4279 in_group(self, name, state::FOCUS_VISIBLE, f)
4280 }
4281}
4282
4283fn transform_with<S: Styled, T: Clone + 'static>(
4287 mut s: S,
4288 value: impl IntoReactive<T>,
4289 step: impl Fn(T) -> (TransformKind, f32) + 'static,
4290) -> S {
4291 let style = s.style();
4294 match value.into_reactive() {
4295 Reactive::Static(value) if !style.is_bound() => {
4296 let (kind, amount) = step(value);
4297 style.push(Declaration::Transform(kind, Bits(amount)));
4298 }
4299 value => style.bind(move |st| {
4300 let (kind, amount) = step(value.get());
4301 st.push(Declaration::Transform(kind, Bits(amount)));
4302 }),
4303 }
4304 s
4305}
4306
4307fn transform_reset<S: Styled>(mut s: S, reset: Declaration) -> S {
4309 let style = s.style();
4310 if style.is_bound() {
4311 style.bind(move |st| st.push(reset.clone()));
4312 } else {
4313 style.push(reset);
4314 }
4315 s
4316}
4317
4318fn translation(
4321 offset: DefiniteLength,
4322 points_kind: TransformKind,
4323 fraction_kind: TransformKind,
4324) -> (TransformKind, f32) {
4325 match offset {
4326 DefiniteLength::Absolute(a) => (points_kind, points(a)),
4327 DefiniteLength::Fraction(f) => (fraction_kind, f.0),
4328 }
4329}
4330
4331fn filter<S: Styled>(mut s: S, backdrop: bool, f: FilterFn, amount: f32) -> S {
4332 s.style()
4333 .push(Declaration::Filter(backdrop, f, Bits(amount)));
4334 s
4335}
4336
4337fn transition<S: Styled>(mut s: S, props: u8) -> S {
4339 s.style().push(Declaration::Transition(props));
4340 s
4341}
4342
4343fn in_group<S: Styled>(
4345 mut s: S,
4346 name: &'static str,
4347 states: u16,
4348 f: impl FnOnce(StyleRefinement) -> StyleRefinement,
4349) -> S {
4350 let styles = f(StyleRefinement::default());
4351 let style = s.style();
4352 let group = style.name(name);
4353 style.extend_as(
4354 Variant {
4355 group_states: states,
4356 group,
4357 ..Variant::default()
4358 },
4359 styles,
4360 );
4361 s
4362}
4363
4364impl Styled for StyleRefinement {
4365 fn style(&mut self) -> &mut StyleRefinement {
4366 self
4367 }
4368}
4369
4370#[cfg(test)]
4371mod tests {
4372 use super::*;
4373 use crate::{
4374 Style,
4375 color::rgba,
4376 element::palette::{EMERALD, PINK, RED, SKY, VIOLET},
4377 geometry::point,
4378 tailwind::{ClassEnvironment, ClassProps, Context},
4379 };
4380
4381 fn resolve(list: &ClassList, dark: bool, states: u16, group: u16) -> String {
4384 resolve_with(list, dark, states, group, &|_| {})
4385 }
4386
4387 fn resolve_with(
4389 list: &ClassList,
4390 dark: bool,
4391 states: u16,
4392 group: u16,
4393 inline: &dyn Fn(&mut Style),
4394 ) -> String {
4395 let cx = Context {
4396 env: ClassEnvironment {
4397 dark,
4398 ..Default::default()
4399 },
4400 window: crate::geometry::size_pt(390.0, 844.0),
4401 rtl: false,
4402 states,
4403 position: 0,
4404 group: &|_| group,
4405 };
4406 let mut style = Style::default();
4407 let mut props = ClassProps::default();
4408 list.apply(&cx, &PRESETS, &mut style, &mut props, inline);
4409 format!("{style:?} {props:?}")
4410 }
4411
4412 #[track_caller]
4415 fn same(typed: StyleRefinement, classes: &str) {
4416 same_with(typed, classes, &|_| {});
4417 }
4418
4419 #[track_caller]
4421 fn same_with(typed: StyleRefinement, classes: &str, inline: &dyn Fn(&mut Style)) {
4422 let typed = typed.lower();
4423 let parsed = tailwind::parse(&PRESETS, classes);
4424 assert!(parsed.unknown().is_empty(), "{:?}", parsed.unknown());
4425 assert_eq!(typed.deps(), parsed.deps(), "{classes}: deps");
4426 assert_eq!(typed.groups(), parsed.groups(), "{classes}: groups");
4427 for dark in [false, true] {
4428 for states in [0, state::PRESSED, state::DISABLED] {
4429 for group in [0, state::PRESSED, state::HOVERED] {
4430 assert_eq!(
4431 resolve(&typed, dark, states, group),
4432 resolve_with(&parsed, dark, states, group, inline),
4433 "{classes} (dark {dark}, states {states}, group {group})"
4434 );
4435 }
4436 }
4437 }
4438 }
4439
4440 fn s() -> StyleRefinement {
4441 StyleRefinement::default()
4442 }
4443
4444 #[test]
4445 fn env_methods_are_the_arbitrary_classes() {
4446 let env_of = |list: &ClassList| {
4447 let cx = Context {
4448 env: ClassEnvironment::default(),
4449 window: crate::geometry::size_pt(390.0, 844.0),
4450 rtl: false,
4451 states: 0,
4452 position: 0,
4453 group: &|_| 0,
4454 };
4455 let mut style = Style::default();
4456 list.apply(
4457 &cx,
4458 &PRESETS,
4459 &mut style,
4460 &mut ClassProps::default(),
4461 |_: &mut Style| {},
4462 );
4463 style.pending.env
4464 };
4465 let sides = |p: &str| {
4466 ["top", "right", "bottom", "left"]
4467 .map(|side| format!("{p}{}-[env(safe-area-inset-{side})]", &side[..1]))
4468 .join(" ")
4469 };
4470 for (typed, classes) in [
4471 (s().p_safe_area(), sides("p")),
4472 (s().m_safe_area(), sides("m")),
4473 (
4474 s().px_safe_area(),
4475 "pr-[env(safe-area-inset-right)] pl-[env(safe-area-inset-left)]".into(),
4476 ),
4477 (
4478 s().my_safe_area(),
4479 "mt-[env(safe-area-inset-top)] mb-[env(safe-area-inset-bottom)]".into(),
4480 ),
4481 (s().pl_safe_area(), "pl-[env(safe-area-inset-left)]".into()),
4482 (
4483 s().pt_safe_area_plus(px(8.0)),
4484 "pt-[calc(env(safe-area-inset-top)+8px)]".into(),
4485 ),
4486 (s().pb_keyboard(), "pb-[env(keyboard-inset-height)]".into()),
4487 (
4488 s().mb_keyboard_plus(px(49.0)),
4489 "mb-[calc(env(keyboard-inset-height)+49px)]".into(),
4490 ),
4491 ] {
4492 same(typed.clone(), &classes);
4493 let parsed = tailwind::parse(&PRESETS, &classes);
4494 assert_eq!(env_of(&typed.lower()), env_of(&parsed), "{classes}");
4495 assert!(!env_of(&parsed).is_empty(), "{classes}");
4496 }
4497 }
4498
4499 #[test]
4500 fn svg_paint_is_tailwinds() {
4501 same(
4502 s().fill_color(RED.shade(600))
4503 .stroke_color(SKY.shade(500))
4504 .stroke_width(px(2.0)),
4505 "fill-red-600 stroke-sky-500 stroke-2",
4506 );
4507 same(s().fill_none().stroke_current(), "fill-none stroke-current");
4508 same(s().fill_current().stroke_none(), "fill-current stroke-none");
4509 }
4510
4511 #[test]
4512 fn flex_is_tailwinds_row() {
4513 same(s().flex(), "flex");
4515 same(s().flex().flex_col(), "flex flex-col");
4516 same(s().flex_col().flex(), "flex-col flex");
4517 same(s().items_center(), "items-center");
4518 }
4519
4520 #[test]
4521 fn auto_minimum_sizes_are_tailwinds() {
4522 same(
4524 s().min_w(crate::geometry::auto())
4525 .min_h(crate::geometry::auto()),
4526 "min-w-auto min-h-auto",
4527 );
4528 }
4529
4530 #[test]
4531 fn borders_and_corners_are_tailwinds() {
4532 same(
4533 s().border_t(px(6.0))
4534 .border_t_color(RED.shade(600))
4535 .border_r(px(2.0))
4536 .border_b(px(4.0))
4537 .border_l(px(1.0))
4538 .border_x_color(SKY.shade(500)),
4539 "border-t-6 border-t-red-600 border-r-2 border-b-4 border-l border-x-sky-500",
4540 );
4541 same(
4542 s().border_y(px(3.0))
4543 .border_s(px(1.0))
4544 .border_e(px(2.0))
4545 .border_dashed(),
4546 "border-y-3 border-s border-e-2 border-dashed",
4547 );
4548 same(s().border_none().border(px(2.0)), "border-none border-2");
4549 same(
4550 s().border(px(3.0)).border_dotted(),
4551 "border-3 border-dotted",
4552 );
4553 same(
4554 s().rounded_tl(px(44.0))
4555 .rounded_tr(px(6.0))
4556 .rounded_br(px(28.0))
4557 .rounded_bl(px(2.0)),
4558 "rounded-tl-[44px] rounded-tr-[6px] rounded-br-[28px] rounded-bl-[2px]",
4559 );
4560 same(
4561 s().rounded_ss(px(22.0))
4562 .rounded_ee(px(8.0))
4563 .rounded_l(px(4.0)),
4564 "rounded-ss-[22px] rounded-ee-[8px] rounded-l-[4px]",
4565 );
4566 same(
4567 s().outline(px(2.0))
4568 .outline_offset(px(2.0))
4569 .outline_dashed()
4570 .outline_color(PINK.shade(500)),
4571 "outline-2 outline-offset-2 outline-dashed outline-pink-500",
4572 );
4573 same(
4574 s().outline_dotted().outline(px(2.0)),
4575 "outline-dotted outline-2",
4576 );
4577 same(s().outline_dashed(), "outline-dashed");
4578 let curved = s().border_curve(BorderCurve::Continuous).lower();
4579 let mut style = Style::default();
4580 let _ = style.set_key(StyleKey::BorderCurve, crate::StyleValue::Str("continuous"));
4581 assert_eq!(
4582 resolve(&curved, false, 0, 0),
4583 format!("{style:?} {:?}", ClassProps::default())
4584 );
4585 }
4586
4587 #[test]
4588 fn shadows_and_rings_are_tailwinds() {
4589 let emerald = EMERALD.shade(500).opacity(0.5);
4590 same(
4591 s().shadow_lg().shadow_color(emerald),
4592 "shadow-lg shadow-emerald-500/50",
4593 );
4594 same(
4595 s().shadow_xl().shadow_color(SKY.shade(500)),
4596 "shadow-xl shadow-sky-500",
4597 );
4598 same(s().inset_shadow_sm(), "inset-shadow-sm");
4599 same(
4600 s().ring(px(2.0))
4601 .ring_color(SKY.shade(500))
4602 .ring_offset(px(2.0)),
4603 "ring-2 ring-sky-500 ring-offset-2",
4604 );
4605 same(
4606 s().ring(px(1.0)).ring_color(rgba(0x0000_000d)).ring_inset(),
4607 "ring-1 ring-black/5 ring-inset",
4608 );
4609 same(s().text_shadow_md(), "text-shadow-md");
4610 let adaptive = s()
4612 .shadow_md()
4613 .shadow_color(AdaptiveColor::new(SKY.shade(500), PINK.shade(500)))
4614 .lower();
4615 assert_eq!(adaptive.deps(), tailwind::deps::DARK);
4616 for (dark, hue) in [(false, SKY), (true, PINK)] {
4617 let classes = format!("shadow-md shadow-{}-500", hue.name());
4618 assert_eq!(
4619 resolve(&adaptive, dark, 0, 0),
4620 resolve(&tailwind::parse(&PRESETS, &classes), dark, 0, 0)
4621 );
4622 }
4623 }
4624
4625 #[test]
4626 fn custom_shadows_are_layers_of_their_own() {
4627 let list = s()
4628 .text_shadow([crate::TextShadow {
4629 color: rgba(0x0000_0099).into(),
4630 offset_x: px(2.0),
4631 offset_y: px(2.0),
4632 radius: px(4.0),
4633 }])
4634 .lower();
4635 let mut style = Style::default();
4636 list.apply(
4637 &Context {
4638 env: ClassEnvironment::default(),
4639 window: crate::geometry::size_pt(390.0, 844.0),
4640 rtl: false,
4641 states: 0,
4642 position: 0,
4643 group: &|_| 0,
4644 },
4645 &PRESETS,
4646 &mut style,
4647 &mut ClassProps::default(),
4648 |_| {},
4649 );
4650 assert_eq!(style.text.shadow_color, Some(rgba(0x0000_0099).into()));
4651 assert_eq!(style.text.shadow_radius, Some(px(4.0)));
4652 assert_eq!(style.text.shadow_offset, Some(point(px(2.0), px(2.0))));
4653 let layer = BoxShadow {
4654 offset_x: px(0.0),
4655 offset_y: px(2.0),
4656 blur: px(4.0),
4657 spread: px(0.0),
4658 color: rgba(0x0000_000d).into(),
4659 inset: true,
4660 };
4661 same(
4662 s().shadow([layer]),
4663 "shadow-[inset_0_2px_4px_rgb(0_0_0/0.05)]",
4664 );
4665 }
4666
4667 #[test]
4668 fn effects_are_tailwinds() {
4669 same(
4670 s().bg_linear_to_br()
4671 .gradient_from(VIOLET.shade(500))
4672 .gradient_via(PINK.shade(500))
4673 .gradient_to(RED.shade(400)),
4674 "bg-linear-to-br from-violet-500 via-pink-500 to-red-400",
4675 );
4676 same(
4677 s().bg_linear(deg(45.0))
4678 .gradient_from(SKY.shade(400))
4679 .gradient_to(VIOLET.shade(600))
4680 .gradient_to_position(percentage(0.8)),
4681 "bg-linear-45 from-sky-400 to-violet-600 to-80%",
4682 );
4683 same(
4684 s().bg_radial().gradient_from(SKY.shade(200)),
4685 "bg-radial from-sky-200",
4686 );
4687 same(
4688 s().bg_conic(deg(0.0)).gradient_from(RED.shade(500)),
4689 "bg-conic from-red-500",
4690 );
4691 same(s().grayscale(1.0).sepia(1.0), "grayscale sepia");
4692 same(s().blur(px(8.0)).brightness(0.5), "blur-sm brightness-50");
4693 same(
4694 s().saturate(1.5).hue_rotate(deg(90.0)),
4695 "saturate-150 hue-rotate-90",
4696 );
4697 same(
4698 s().backdrop_blur(px(12.0))
4699 .backdrop_saturate(1.5)
4700 .backdrop_sepia(1.0),
4701 "backdrop-blur-md backdrop-saturate-150 backdrop-sepia",
4702 );
4703 same(
4704 s().mix_blend_mode(BlendMode::Multiply),
4705 "mix-blend-multiply",
4706 );
4707 same(s().transition(), "transition");
4708 same(
4709 s().transition_colors()
4710 .duration(150.0)
4711 .ease_in_out()
4712 .delay(75.0),
4713 "transition-colors duration-150 ease-in-out delay-75",
4714 );
4715 same(
4716 s().transition_transform().duration(200.0),
4717 "transition-transform duration-200",
4718 );
4719 for (typed, classes) in [
4720 (s().animate_spin(), "animate-spin"),
4721 (s().animate_ping(), "animate-ping"),
4722 (s().animate_pulse(), "animate-pulse"),
4723 (s().animate_bounce(), "animate-bounce"),
4724 ] {
4725 same(typed, classes);
4726 }
4727 }
4728
4729 #[test]
4730 fn keyframe_animations_take_their_own_timing() {
4731 let timed = |name: &'static str, ms: f32, easing: &'static str| {
4733 move |style: &mut Style| {
4734 use crate::StyleValue;
4735 style
4736 .set_key(StyleKey::AnimationName, StyleValue::Str(name))
4737 .unwrap();
4738 style
4739 .set_key(StyleKey::AnimationDuration, StyleValue::Number(ms))
4740 .unwrap();
4741 style
4742 .set_key(StyleKey::AnimationTimingFunction, StyleValue::Str(easing))
4743 .unwrap();
4744 }
4745 };
4746 same_with(
4747 s().animation(KeyframeAnimation::new(Keyframes::Spin).duration(800.0)),
4748 "animate-none",
4749 &timed("spin", 800.0, "linear"),
4750 );
4751 same_with(
4752 s().animation(
4753 KeyframeAnimation::new(Keyframes::Pulse)
4754 .duration(900.0)
4755 .easing([0.42, 0.0, 0.58, 1.0]),
4756 ),
4757 "animate-none",
4758 &timed("pulse", 900.0, "ease-in-out"),
4759 );
4760 same(
4762 s().animation(KeyframeAnimation::new(Keyframes::Spin).duration(800.0))
4763 .animate_ping(),
4764 "animate-ping",
4765 );
4766 same_with(
4767 s().animate_ping()
4768 .animation(KeyframeAnimation::new(Keyframes::Spin).duration(800.0)),
4769 "animate-none",
4770 &timed("spin", 800.0, "linear"),
4771 );
4772 }
4773
4774 #[test]
4775 fn keyframe_animations_take_their_frame_rate() {
4776 let capped = |hz: f32| {
4778 move |style: &mut Style| {
4779 use crate::StyleValue;
4780 for (key, value) in [
4781 (StyleKey::AnimationName, StyleValue::Str("spin")),
4782 (StyleKey::AnimationDuration, StyleValue::Number(800.0)),
4783 (StyleKey::AnimationTimingFunction, StyleValue::Str("linear")),
4784 (StyleKey::AnimationFrameRate, StyleValue::Number(hz)),
4785 ] {
4786 style.set_key(key, value).unwrap();
4787 }
4788 }
4789 };
4790 same_with(
4791 s().animation(
4792 KeyframeAnimation::new(Keyframes::Spin)
4793 .duration(800.0)
4794 .frame_rate(30.0),
4795 ),
4796 "animate-none",
4797 &capped(30.0),
4798 );
4799 let typed = s()
4800 .animation(KeyframeAnimation::new(Keyframes::Spin).frame_rate(24.0))
4801 .lower();
4802 assert!(resolve(&typed, false, 0, 0).contains("frame_rate: Some(24.0)"));
4803 }
4804
4805 #[test]
4806 fn transforms_are_tailwinds() {
4807 same(
4808 s().rotate(deg(12.0)).skew_x(deg(6.0)).scale(0.9),
4809 "rotate-12 skew-x-6 scale-90",
4810 );
4811 same(
4812 s().perspective(px(400.0))
4813 .rotate_y(deg(35.0))
4814 .rotate_x(deg(40.0)),
4815 "perspective-[400px] rotate-y-35 rotate-x-40",
4816 );
4817 same(s().scale_x(1.5).skew_y(deg(-3.0)), "scale-x-150 -skew-y-3");
4818 same(
4819 s().rotate(deg(12.0))
4820 .rotate_x(deg(40.0))
4821 .skew_x(deg(6.0))
4822 .scale(0.9)
4823 .transform_none(),
4824 "rotate-12 rotate-x-40 skew-x-6 scale-90 transform-none",
4825 );
4826 same(
4827 s().translate_x(px(8.0))
4828 .rotate(deg(12.0))
4829 .scale(0.9)
4830 .translate_none()
4831 .rotate_none()
4832 .scale_none(),
4833 "translate-x-2 rotate-12 scale-90 translate-none rotate-none scale-none",
4834 );
4835 same(
4836 s().origin_top_left().backface_hidden(),
4837 "origin-top-left backface-hidden",
4838 );
4839 same(
4840 s().transform_origin(TransformOrigin {
4841 x: px(10.0).into(),
4842 y: relative(0.25),
4843 z: Points::ZERO,
4844 }),
4845 "origin-[10px_25%]",
4846 );
4847 }
4848
4849 #[test]
4850 fn text_is_tailwinds() {
4851 same(
4852 s().underline()
4853 .text_decoration_wavy()
4854 .text_decoration_color(RED.shade(500))
4855 .text_decoration_thickness(px(2.0))
4856 .underline_offset(px(4.0)),
4857 "underline decoration-wavy decoration-red-500 decoration-2 underline-offset-4",
4858 );
4859 same(s().uppercase().tabular_nums(), "uppercase tabular-nums");
4860 same(s().small_caps().oldstyle_nums(), "small-caps oldstyle-nums");
4861 same(s().capitalize().text_justify(), "capitalize text-justify");
4862 same(s().align_baseline(), "align-baseline");
4863 same(s().align_text_top(), "align-text-top");
4864 same(s().align_text_bottom(), "align-text-bottom");
4865 same(s().align_middle(), "align-middle");
4866 let both = s().underline().line_through().lower();
4868 let mut style = Style::default();
4869 let _ = style.set_key(
4870 StyleKey::TextDecorationLine,
4871 crate::StyleValue::Str("underline line-through"),
4872 );
4873 assert_eq!(
4874 resolve(&both, false, 0, 0),
4875 format!("{style:?} {:?}", ClassProps::default())
4876 );
4877 same(
4878 s().underline().line_through().no_underline(),
4879 "no-underline",
4880 );
4881 let rtl = s().writing_direction(WritingDirection::Rtl).lower();
4882 let mut style = Style::default();
4883 let _ = style.set_key(StyleKey::WritingDirection, crate::StyleValue::Str("rtl"));
4884 assert_eq!(
4885 resolve(&rtl, false, 0, 0),
4886 format!("{style:?} {:?}", ClassProps::default())
4887 );
4888 }
4889
4890 #[test]
4891 fn layout_is_tailwinds() {
4892 same(s().grid().grid_cols(3).gap_2(), "grid grid-cols-3 gap-2");
4893 same(s().col_span(2).row_span(2), "col-span-2 row-span-2");
4894 same(s().col_start(2).col_end(-1), "col-start-2 col-end-[-1]");
4895 same(s().flex_grow(2.0).flex_shrink(0.0), "grow-2 shrink-0");
4896 same(
4897 s().grid_rows(2).grid_flow_col(),
4898 "grid-rows-2 grid-flow-col",
4899 );
4900 }
4901
4902 #[test]
4903 fn variants_are_tailwinds() {
4904 same(
4905 s().group_active(|s| s.text_color(VIOLET.shade(600)).translate_x(px(4.0))),
4906 "group-active:text-violet-600 group-active:translate-x-1",
4907 );
4908 same(s().group(), "group");
4909 same(
4910 s().group_named("item")
4911 .group_hover_named("item", |s| s.opacity(0.5)),
4912 "group/item group-hover/item:opacity-50",
4913 );
4914 same(s().dark(|s| s.shadow_none()), "dark:shadow-none");
4915 let mut disabled = s();
4916 disabled.extend_as(Variant::DISABLED, s().opacity(0.4));
4917 same(disabled, "disabled:opacity-40");
4918 let mut nested = s();
4920 nested.extend_as(Variant::ACTIVE, s().dark(|s| s.opacity(0.5)));
4921 same(nested, "dark:active:opacity-50");
4922 }
4923}