yew_quick 0.2.2

yew 框架快速开发组件库
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use yew::{
    function_component, html, use_memo, Callback, Children, Html, MouseEvent, NodeRef, Properties,
};

use stylist::css;

use crate::prelude::{
    Cursor, Display, FontStyle, FontWeight, TextAlign, TimingFn, WhiteSpace, WordBreak,
};
use crate::utils::{add_op_space, is_have_unit};

#[derive(Clone, PartialEq)]
struct TextCss {
    display: String,
    width: String,
    height: String,
    padding: String,
    margin: String,

    background_image: String,
    cursor: String,
    white_space: String,
    min_width: String,
    min_height: String,
    max_width: String,
    max_height: String,

    z_index: String,
    opacity: String,

    font_size: String,
    color: String,
    font_style: String,
    font_weight: String,
    letter_spacing: String,
    line_height: String,
    text_decoration: String,
    text_align: String,
    word_break: String,

    duration: String,
    timing_fn: String,
    hover_opacity: String,
    hover_padding: String,
    hover_margin: String,
    hover_color: String,
    hover_width: String,
    hover_height: String,

    dark_color: String,
}

#[derive(Clone, PartialEq)]
struct TextCssProps {
    display: Display,
    size: String,
    padding: String,
    margin: String,

    bg_image: String,
    cursor: Cursor,
    white_space: WhiteSpace,
    min_size: String,
    max_size: String,

    z_index: String,
    opacity: String,

    font_size: String,
    color: String,
    font_style: FontStyle,
    font_weight: FontWeight,
    letter_spacing: String,
    line_height: String,
    text_decoration: String,
    text_align: TextAlign,
    word_break: WordBreak,

    duration: String,
    timing_fn: TimingFn,
    h_opacity: String,
    h_padding: String,
    h_margin: String,
    h_color: String,
    h_size: String,

    d_color: String,
}

#[derive(Properties, Clone, PartialEq)]
pub struct TextProps {
    #[prop_or(Display::Block)]
    pub display: Display,
    #[prop_or(String::from("auto auto"))]
    pub size: String,
    #[prop_or(String::from("0"))]
    pub padding: String,
    #[prop_or(String::from("0"))]
    pub margin: String,

    #[prop_or(String::from("0"))]
    pub bg_image: String,

    #[prop_or(Cursor::Unset)]
    pub cursor: Cursor,
    #[prop_or(WhiteSpace::Normal)]
    pub white_space: WhiteSpace,
    #[prop_or(String::from("auto auto"))]
    pub min_size: String,
    #[prop_or(String::from("auto auto"))]
    pub max_size: String,

    #[prop_or(String::from("auto"))]
    pub z_index: String,
    #[prop_or(String::from("inherit"))]
    pub opacity: String,

    #[prop_or(String::from("medium"))]
    pub font_size: String,
    #[prop_or(String::from("#181818"))]
    pub color: String,
    #[prop_or(FontStyle::Normal)]
    pub font_style: FontStyle,
    #[prop_or(FontWeight::Normal)]
    pub font_weight: FontWeight,
    #[prop_or(String::from("normal"))]
    pub letter_spacing: String,
    #[prop_or(String::from("normal"))]
    pub line_height: String,
    #[prop_or(String::from("none"))]
    pub text_decoration: String,
    #[prop_or(TextAlign::Left)]
    pub text_align: TextAlign,
    #[prop_or(WordBreak::Normal)]
    pub word_break: WordBreak,

    #[prop_or(String::from("0"))]
    pub duration: String,
    #[prop_or(TimingFn::Ease)]
    pub timing_fn: TimingFn,
    #[prop_or(String::from(""))]
    pub h_opacity: String,
    #[prop_or(String::from(""))]
    pub h_padding: String,
    #[prop_or(String::from(""))]
    pub h_margin: String,
    #[prop_or(String::from(""))]
    pub h_color: String,
    #[prop_or(String::from(""))]
    pub h_size: String,

    #[prop_or(String::from(""))]
    pub d_color: String,

    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub onclick: Callback<MouseEvent>,
    #[prop_or_default]
    pub node: NodeRef,
}

/// ### 使用示例
///
///```
/// size: String,
/// padding: String,
/// margin: String,
/// bg_image: String,  // linear-gradient, 文字渐变
/// cursor: Cursor,
/// white_space: WhiteSpace,
/// min_size: String,
/// max_size: String,
/// z_index: String,
/// opacity: String,
/// font_size: String,
/// color: String,
/// font_style: FontStyle,
/// font_weight: FontWeight,
/// letter_spacing: String,
/// line_height: String,
/// text_decoration: String,
/// text_align: TextAlign,
/// word_break: WordBreak,
/// duration: String,
/// timing_fn: TimingFn, // transition 的动画方式
/// h_opacity: String,  //hover 样式 "0.7"
/// h_padding: String,  //hover 样式 "0 0 12 12"
/// h_margin: String,  //hover 样式 "0 0 12 12"
/// h_color: String, // hover 时的文字颜色
/// h_size: String,  // hover 时的长宽
/// d_color: String, // dark 模式
///```
#[function_component]
pub fn Text(props: &TextProps) -> Html {
    let box_css_p: TextCssProps = TextCssProps {
        display: props.display.clone(),
        size: props.size.clone(),
        padding: props.padding.clone(),
        margin: props.margin.clone(),

        bg_image: props.bg_image.clone(),

        cursor: props.cursor.clone(),
        white_space: props.white_space.clone(),
        min_size: props.min_size.clone(),
        max_size: props.max_size.clone(),

        z_index: props.z_index.clone(),
        opacity: props.opacity.clone(),

        font_size: props.font_size.clone(),
        color: props.color.clone(),
        font_style: props.font_style.clone(),
        font_weight: props.font_weight.clone(),
        letter_spacing: props.letter_spacing.clone(),
        line_height: props.line_height.clone(),
        text_decoration: props.text_decoration.clone(),
        text_align: props.text_align.clone(),
        word_break: props.word_break.clone(),

        duration: props.duration.clone(),
        timing_fn: props.timing_fn.clone(),
        h_opacity: props.h_opacity.clone(),
        h_padding: props.h_padding.clone(),
        h_margin: props.h_margin.clone(),
        h_color: props.h_color.clone(),
        h_size: props.h_size.clone(),

        d_color: props.d_color.clone(),
    };

    let box_css = use_memo(
        |box_css_p| {
            let temp_size = box_css_p.size.split(" ").collect::<Vec<&str>>();
            let temp_h_size = if box_css_p.h_size == String::default() {
                temp_size.clone()
            } else {
                box_css_p.h_size.split(" ").collect::<Vec<&str>>()
            };
            let temp_min_size = box_css_p.min_size.split(" ").collect::<Vec<&str>>();
            let temp_max_size = box_css_p.max_size.split(" ").collect::<Vec<&str>>();

            let temp_width = temp_size[0];
            let temp_height = if temp_size.len() == 1 {
                temp_size[0]
            } else {
                temp_size[1]
            };

            let temp_h_width = temp_h_size[0];
            let temp_h_height = if temp_h_size.len() == 1 {
                temp_h_size[0]
            } else {
                temp_h_size[1]
            };

            let temp_min_width = temp_min_size[0];
            let temp_min_height = if temp_min_size.len() == 1 {
                temp_min_size[0]
            } else {
                temp_min_size[1]
            };
            let temp_max_width = temp_max_size[0];
            let temp_max_height = if temp_max_size.len() == 1 {
                temp_max_size[0]
            } else {
                temp_max_size[1]
            };

            let temp_width_op = add_op_space(temp_width);
            let tmep_width_f = if temp_width.contains("%") {
                format!("{}", temp_width)
            } else {
                format!("{}{}", temp_width, "px")
            };
            let temp_height_op = add_op_space(temp_height);
            let temp_height_f = if temp_height.contains("%") {
                format!("{}", temp_height)
            } else {
                format!("{}{}", temp_height, "px")
            };

            let temp_h_width_op = add_op_space(temp_h_width);
            let tmep_h_width_f = if temp_h_width.contains("%") {
                format!("{}", temp_h_width)
            } else {
                format!("{}{}", temp_h_width, "px")
            };
            let temp_h_height_op = add_op_space(temp_h_height);
            let temp_h_height_f = if temp_h_height.contains("%") {
                format!("{}", temp_h_height)
            } else {
                format!("{}{}", temp_h_height, "px")
            };

            let temp_min_width_op = add_op_space(temp_min_width);
            let tmep_min_width_f = if temp_min_width.contains("%") {
                format!("{}", temp_min_width)
            } else {
                format!("{}{}", temp_min_width, "px")
            };
            let temp_min_height_op = add_op_space(temp_min_height);
            let temp_min_height_f = if temp_min_height.contains("%") {
                format!("{}", temp_min_height)
            } else {
                format!("{}{}", temp_min_height, "px")
            };
            let temp_max_width_op = add_op_space(temp_max_width);
            let tmep_max_width_f = if temp_max_width.contains("%") {
                format!("{}", temp_max_width)
            } else {
                format!("{}{}", temp_max_width, "px")
            };
            let temp_max_height_op = add_op_space(temp_max_height);
            let temp_max_height_f = if temp_max_height.contains("%") {
                format!("{}", temp_max_height)
            } else {
                format!("{}{}", temp_max_height, "px")
            };

            let temp_padding = box_css_p.padding.as_str();
            let temp_margin = box_css_p.margin.as_str();

            let temp_padding_c = temp_padding
                .split(" ")
                .map(|x| {
                    if x.contains("%") {
                        x.to_string()
                    } else {
                        x.to_string() + "px"
                    }
                })
                .collect::<Vec<String>>()
                .join(" ");
            let padding_value = if is_have_unit(temp_padding) {
                temp_padding.to_owned()
            } else {
                temp_padding_c
            };
            let temp_margin_c = temp_margin
                .split(" ")
                .map(|x| {
                    if x.contains("%") {
                        x.to_string()
                    } else {
                        x.to_string() + "px"
                    }
                })
                .collect::<Vec<String>>()
                .join(" ");
            let margin_value = if is_have_unit(temp_margin) {
                temp_margin.to_owned()
            } else {
                temp_margin_c
            };

            TextCss {
                display: box_css_p.display.get_name(),
                width: if temp_width == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_width) {
                    temp_width_op
                } else {
                    tmep_width_f
                },
                height: if temp_height == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_height) {
                    temp_height_op
                } else {
                    temp_height_f
                },
                padding: padding_value.clone(),
                margin: margin_value.clone(),

                background_image: if box_css_p.bg_image == "0" {
                    "none".to_owned()
                } else {
                    if box_css_p.bg_image.contains("linear-gradient") {
                        box_css_p.bg_image.clone()
                    } else {
                        "url(".to_string() + box_css_p.bg_image.clone().as_str() + ")"
                    }
                },
                cursor: box_css_p.cursor.get_name(),
                white_space: box_css_p.white_space.get_name(),
                min_width: if temp_min_width == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_min_width) {
                    temp_min_width_op
                } else {
                    tmep_min_width_f
                },
                min_height: if temp_min_height == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_min_height) {
                    temp_min_height_op
                } else {
                    temp_min_height_f
                },
                max_width: if temp_max_width == "auto" {
                    "none".to_owned()
                } else if is_have_unit(temp_max_width) {
                    temp_max_width_op
                } else {
                    tmep_max_width_f
                },
                max_height: if temp_max_height == "auto" {
                    "none".to_owned()
                } else if is_have_unit(temp_max_height) {
                    temp_max_height_op
                } else {
                    temp_max_height_f
                },
                z_index: box_css_p.z_index.clone(),
                opacity: box_css_p.opacity.clone(),

                font_size: if is_have_unit(&box_css_p.font_size) {
                    box_css_p.font_size.to_owned()
                } else {
                    box_css_p.font_size.clone() + "px"
                },
                color: box_css_p.color.clone(),
                font_style: box_css_p.font_style.get_name(),
                font_weight: box_css_p.font_weight.get_name(),
                letter_spacing: if is_have_unit(&box_css_p.letter_spacing) {
                    box_css_p.letter_spacing.to_owned()
                } else {
                    box_css_p.letter_spacing.clone() + "px"
                },
                line_height: if is_have_unit(&box_css_p.line_height) {
                    box_css_p.line_height.to_owned()
                } else {
                    box_css_p.line_height.clone() + "px"
                },
                text_decoration: box_css_p.text_decoration.clone(),
                text_align: box_css_p.text_align.get_name(),
                word_break: box_css_p.word_break.get_name(),

                duration: box_css_p.duration.clone(),
                timing_fn: box_css_p.timing_fn.get_name(),
                hover_opacity: if box_css_p.h_opacity == String::default() {
                    box_css_p.opacity.clone()
                } else {
                    box_css_p.h_opacity.clone()
                },
                hover_padding: if box_css_p.h_padding == String::default() {
                    padding_value
                } else {
                    let temp = box_css_p.h_padding.as_str();
                    if is_have_unit(temp) {
                        temp.to_string()
                    } else {
                        temp.split(" ")
                            .map(|x| {
                                if x.contains("%") {
                                    x.to_string()
                                } else {
                                    x.to_string() + "px"
                                }
                            })
                            .collect::<Vec<String>>()
                            .join(" ")
                    }
                },
                hover_margin: if box_css_p.h_margin == String::default() {
                    margin_value
                } else {
                    let temp = box_css_p.h_margin.as_str();
                    if is_have_unit(temp) {
                        temp.to_string()
                    } else {
                        temp.split(" ")
                            .map(|x| {
                                if x.contains("%") {
                                    x.to_string()
                                } else {
                                    x.to_string() + "px"
                                }
                            })
                            .collect::<Vec<String>>()
                            .join(" ")
                    }
                },
                hover_color: if box_css_p.h_color == String::default() {
                    "none".to_string()
                } else {
                    box_css_p.h_color.clone()
                },
                hover_width: if temp_h_width == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_h_width) {
                    temp_h_width_op
                } else {
                    tmep_h_width_f
                },
                hover_height: if temp_h_height == "auto" {
                    "auto".to_owned()
                } else if is_have_unit(temp_h_height) {
                    temp_h_height_op
                } else {
                    temp_h_height_f
                },
                dark_color: if box_css_p.d_color == String::default() {
                    box_css_p.color.clone()
                } else {
                    box_css_p.d_color.clone()
                },
            }
        },
        box_css_p,
    );

    let class = css!(
        r#"
            display: ${display};
            width: ${width};
            height: ${height};
            padding: ${padding};
            margin: ${margin};

            background-image: ${bg_img};
            cursor: ${cursor};
            white-space: ${white_space};
            min-width: ${min_width};
            min-height: ${min_height};
            max-width: ${max_width};
            max-height: ${max_height};
            z-index: ${z_index};
            opacity: ${opacity};

            font-size: ${font_size};
            color: ${color};
            font-style: ${font_style};
            font-weight: ${font_weight};
            letter-spacing: ${letter_spacing};
            line-height: ${line_height};
            text-decoration: ${text_decoration};
            text-align: ${text_align};
            word-break: ${word_break};

            transition: all ${duration}s ${timing_fn};
            &:hover {
                color: ${hover_color};
                width: ${hover_width};
                height: ${hover_height};
                padding: ${hover_padding};
                margin: ${hover_margin};
                opacity: ${hover_opacity};
            }

            @media (prefers-color-scheme: dark) {
              & {
                color: ${dark_color};
              }
            }
        "#,
        display = box_css.display,
        width = box_css.width,
        height = box_css.height,
        padding = box_css.padding,
        margin = box_css.margin,
        bg_img = box_css.background_image,
        cursor = box_css.cursor,
        white_space = box_css.white_space,
        min_width = box_css.min_width,
        min_height = box_css.min_height,
        max_width = box_css.max_width,
        max_height = box_css.max_height,
        z_index = box_css.z_index,
        opacity = box_css.opacity,
        font_size = box_css.font_size,
        color = box_css.color,
        font_style = box_css.font_style,
        font_weight = box_css.font_weight,
        letter_spacing = box_css.letter_spacing,
        line_height = box_css.line_height,
        text_decoration = box_css.text_decoration,
        text_align = box_css.text_align,
        word_break = box_css.word_break,
        duration = box_css.duration,
        timing_fn = box_css.timing_fn,
        hover_opacity = box_css.hover_opacity,
        hover_padding = box_css.hover_padding,
        hover_margin = box_css.hover_margin,
        hover_color = box_css.hover_color,
        hover_width = box_css.hover_width,
        hover_height = box_css.hover_height,
        dark_color = box_css.dark_color,
    );

    html! {
        <span {class} onclick={props.onclick.clone()} ref={props.node.clone()}>
        { for props.children.iter() }
        </span>
    }
}