| | 1 | | using System.Text.RegularExpressions; |
| | 2 | |
|
| | 3 | | namespace Allyaria.Theming.Styles; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Represents a combined Allyaria style that encapsulates both palette and typography settings. Provides methods to |
| | 7 | | /// generate CSS fragments and CSS custom property declarations for applying the style, including distinct "hover" |
| | 8 | | /// variants. |
| | 9 | | /// </summary> |
| | 10 | | public readonly record struct AllyariaStyle |
| | 11 | | { |
| | 12 | | /// <summary>Initializes a new instance of the <see cref="AllyariaStyle" /> struct.</summary> |
| | 13 | | /// <param name="palette">The color palette to apply.</param> |
| | 14 | | /// <param name="typography">The typography settings to apply.</param> |
| | 15 | | /// <param name="paletteHover"> |
| | 16 | | /// The color palette to apply for hover effects. If null, it is derived from <paramref name="palette" />. |
| | 17 | | /// </param> |
| | 18 | | /// <param name="typographyHover"> |
| | 19 | | /// The typography settings to apply for hover effects. Defaults to |
| | 20 | | /// <paramref name="typography" />. |
| | 21 | | /// </param> |
| | 22 | | /// <param name="paletteDisabled"> |
| | 23 | | /// The color palette to apply for disabled state. If null, it is derived from <paramref name="palette" />. |
| | 24 | | /// </param> |
| | 25 | | /// <param name="typographyDisabled"> |
| | 26 | | /// The typography settings to apply for disabled state. Defaults to |
| | 27 | | /// <paramref name="typography" />. |
| | 28 | | /// </param> |
| | 29 | | public AllyariaStyle(AllyariaPalette palette, |
| | 30 | | AllyariaTypography typography, |
| | 31 | | AllyariaPalette? paletteHover = null, |
| | 32 | | AllyariaTypography? typographyHover = null, |
| | 33 | | AllyariaPalette? paletteDisabled = null, |
| | 34 | | AllyariaTypography? typographyDisabled = null) |
| | 35 | | { |
| 20 | 36 | | Palette = palette; |
| 20 | 37 | | Typography = typography; |
| 20 | 38 | | PaletteHover = paletteHover ?? palette.ToHoverPalette(); |
| 20 | 39 | | TypographyHover = typographyHover ?? typography; |
| 20 | 40 | | PaletteDisabled = paletteDisabled ?? palette.ToDisabledPalette(); |
| 20 | 41 | | TypographyDisabled = typographyDisabled ?? typography; |
| 20 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary>Gets the color palette to apply.</summary> |
| 10 | 45 | | public AllyariaPalette Palette { get; } |
| | 46 | |
|
| | 47 | | /// <summary>Gets the color palette to apply for disabled state.</summary> |
| 14 | 48 | | public AllyariaPalette PaletteDisabled { get; } |
| | 49 | |
|
| | 50 | | /// <summary>Gets the color palette to apply for hover effects.</summary> |
| 16 | 51 | | public AllyariaPalette PaletteHover { get; } |
| | 52 | |
|
| | 53 | | /// <summary>Gets the typography settings to apply.</summary> |
| 10 | 54 | | public AllyariaTypography Typography { get; } |
| | 55 | |
|
| | 56 | | /// <summary>Gets the typography settings to apply for disabled state.</summary> |
| 8 | 57 | | public AllyariaTypography TypographyDisabled { get; } |
| | 58 | |
|
| | 59 | | /// <summary>Gets the typography settings to apply for hover effects.</summary> |
| 14 | 60 | | public AllyariaTypography TypographyHover { get; } |
| | 61 | |
|
| | 62 | | /// <summary>Builds the full CSS string for this style, combining palette and typography CSS.</summary> |
| | 63 | | /// <returns>A concatenated CSS string including palette and typography CSS.</returns> |
| 2 | 64 | | public string ToCss() => string.Concat(Palette.ToCss(), Typography.ToCss()); |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Builds the full CSS string for the <c>:hover</c> state, combining hover palette and hover typography CSS. |
| | 68 | | /// </summary> |
| | 69 | | /// <returns>A concatenated CSS string including hover palette and hover typography CSS.</returns> |
| 2 | 70 | | public string ToCssHover() => string.Concat(PaletteHover.ToCss(), TypographyHover.ToCss()); |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Builds a CSS string for custom property (variable) declarations representing this style and its hover variants. |
| | 74 | | /// optional <paramref name="prefix" /> is normalized by trimming whitespace and dashes, converting to lowercase, an |
| | 75 | | /// replacing spaces with hyphens. If no usable prefix remains, variables are emitted with the default <c>aa</c> pre |
| | 76 | | /// Hover prefix appends <c>-hover</c> to the prefix. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="prefix"> |
| | 79 | | /// An optional namespace for the CSS variables (e.g., <c>"editor"</c>). Spaces and control characters are replaced |
| | 80 | | /// hyphens. If empty or whitespace, defaults to <c>aa</c>. Hover prefix appends <c>-hover</c> to the prefix. |
| | 81 | | /// </param> |
| | 82 | | /// <returns> |
| | 83 | | /// A concatenated CSS string that includes palette and typography variable declarations for both base and hover sta |
| | 84 | | /// using the same normalized prefix. |
| | 85 | | /// </returns> |
| | 86 | | /// <remarks> |
| | 87 | | /// This method composes the results of <see cref="AllyariaPalette.ToCssVars(string)" /> and |
| | 88 | | /// <see cref="AllyariaTypography.ToCssVars(string)" /> with a normalized prefix. Hover variables are emitted with a |
| | 89 | | /// additional <c>-hover</c> suffix appended to the base prefix. |
| | 90 | | /// </remarks> |
| | 91 | | public string ToCssVars(string prefix = "") |
| | 92 | | { |
| 8 | 93 | | var basePrefix = Regex.Replace(prefix, @"[\s-]+", "-").Trim('-').ToLowerInvariant(); |
| | 94 | |
|
| 8 | 95 | | if (string.IsNullOrWhiteSpace(basePrefix)) |
| | 96 | | { |
| 4 | 97 | | basePrefix = "aa"; |
| | 98 | | } |
| | 99 | |
|
| 8 | 100 | | var disabledPrefix = $"{basePrefix}-disabled"; |
| 8 | 101 | | var hoverPrefix = $"{basePrefix}-hover"; |
| | 102 | |
|
| 8 | 103 | | return string.Concat( |
| 8 | 104 | | Palette.ToCssVars(basePrefix), |
| 8 | 105 | | Typography.ToCssVars(basePrefix), |
| 8 | 106 | | PaletteDisabled.ToCssVars(disabledPrefix), |
| 8 | 107 | | TypographyDisabled.ToCssVars(disabledPrefix), |
| 8 | 108 | | PaletteHover.ToCssVars(hoverPrefix), |
| 8 | 109 | | TypographyHover.ToCssVars(hoverPrefix) |
| 8 | 110 | | ); |
| | 111 | | } |
| | 112 | | } |