|
| 1 | +module Lumi.Components2.Text |
| 2 | + ( Text |
| 3 | + , TextProps, TextType |
| 4 | + , text |
| 5 | + , TextModifier, body, strong, emphasized, subtext |
| 6 | + , muted, color |
| 7 | + |
| 8 | + , ParagraphProps |
| 9 | + , paragraph_, paragraph |
| 10 | + |
| 11 | + , Header |
| 12 | + , HeaderProps |
| 13 | + , subsectionHeader |
| 14 | + , sectionHeader |
| 15 | + , title |
| 16 | + , mainHeader |
| 17 | + ) where |
| 18 | + |
| 19 | +import Prelude |
| 20 | + |
| 21 | +import Data.Maybe (Maybe(..), fromMaybe, maybe) |
| 22 | +import Effect.Unsafe (unsafePerformEffect) |
| 23 | +import Lumi.Components (LumiComponent, PropsModifier, PropsModifier', lumiComponent, propsModifier) |
| 24 | +import Lumi.Components.Color (ColorMap) |
| 25 | +import Lumi.Styles (Style, StyleModifier, StyleProperty) |
| 26 | +import Lumi.Styles as S |
| 27 | +import Lumi.Styles.Theme (LumiTheme(..), TextMap, textFontSize, textLineHeight, textMargin, useTheme) |
| 28 | +import React.Basic (JSX, ReactComponent) |
| 29 | +import React.Basic.DOM as R |
| 30 | +import React.Basic.Emotion as E |
| 31 | +import React.Basic.Hooks as Hooks |
| 32 | + |
| 33 | +-- Text |
| 34 | + |
| 35 | +data Text = Text |
| 36 | + |
| 37 | +data TextType |
| 38 | + = Body |
| 39 | + | Strong |
| 40 | + | Emphasis |
| 41 | + | Subtext |
| 42 | + |
| 43 | +type TextProps = |
| 44 | + ( component :: Text |
| 45 | + , type :: Maybe TextType |
| 46 | + , content :: String |
| 47 | + ) |
| 48 | + |
| 49 | +type TextElement = ReactComponent { children :: Array JSX, className :: String } |
| 50 | + |
| 51 | +-- | TODO: write documentation |
| 52 | +text :: LumiComponent TextProps |
| 53 | +text = |
| 54 | + unsafePerformEffect $ lumiComponent "Text" defaults \props -> Hooks.do |
| 55 | + theme <- useTheme |
| 56 | + pure $ |
| 57 | + E.element (maybe R.span' textElement props.type) |
| 58 | + { children: [ R.text props.content ] |
| 59 | + , className: props.className |
| 60 | + , css: defaultTextStyle theme props.type <> props.css theme |
| 61 | + } |
| 62 | + where |
| 63 | + defaults :: Record TextProps |
| 64 | + defaults = |
| 65 | + { component: Text |
| 66 | + , type: Nothing |
| 67 | + , content: "" |
| 68 | + } |
| 69 | + |
| 70 | + defaultTextStyle :: LumiTheme -> Maybe TextType -> Style |
| 71 | + defaultTextStyle theme ty = |
| 72 | + S.css |
| 73 | + { fontSize: maybe S.inherit (px <<< textFontSize theme <<< textTheme) ty |
| 74 | + , lineHeight: maybe S.inherit (px <<< textLineHeight theme <<< textTheme) ty |
| 75 | + , whiteSpace: S.str "pre-wrap" |
| 76 | + , margin: S.str "0" |
| 77 | + , padding: S.str "0" |
| 78 | + } |
| 79 | + |
| 80 | + textElement :: TextType -> TextElement |
| 81 | + textElement Body = R.span' |
| 82 | + textElement Strong = R.strong' |
| 83 | + textElement Emphasis = R.em' |
| 84 | + textElement Subtext = R.small' |
| 85 | + |
| 86 | +textTheme :: TextType -> (forall a. TextMap a -> a) |
| 87 | +textTheme = |
| 88 | + case _ of |
| 89 | + Body -> _.body |
| 90 | + Strong -> _.body |
| 91 | + Emphasis -> _.body |
| 92 | + Subtext -> _.subtext |
| 93 | + |
| 94 | +-- | The `c` type parameter lets us constrain the type of component to which |
| 95 | +-- | a text modifier may be applied: `Text`, `Header` or any. |
| 96 | +type TextModifier c = forall r. PropsModifier (component :: c, type :: Maybe TextType | r) |
| 97 | + |
| 98 | +body :: TextModifier Text |
| 99 | +body = propsModifier _{ type = Just Body } |
| 100 | + |
| 101 | +strong :: forall c. TextModifier c |
| 102 | +strong = |
| 103 | + propsModifier _{ type = Just Strong } |
| 104 | + <<< S.style_ (S.css { fontWeight: S.str "600" }) |
| 105 | + |
| 106 | +emphasized :: forall c. TextModifier c |
| 107 | +emphasized = |
| 108 | + propsModifier _{ type = Just Emphasis } |
| 109 | + <<< S.style_ (S.css { fontStyle: S.str "italic" }) |
| 110 | + |
| 111 | +subtext :: TextModifier Text |
| 112 | +subtext = propsModifier _{ type = Just Subtext } |
| 113 | + |
| 114 | +muted :: forall c. TextModifier c |
| 115 | +muted = |
| 116 | + S.style \(LumiTheme { colors }) -> |
| 117 | + S.css { color: S.color colors.black1 } |
| 118 | + |
| 119 | +color :: forall c. (forall a. ColorMap a -> a) -> TextModifier c |
| 120 | +color f = |
| 121 | + S.style \(LumiTheme { colors }) -> |
| 122 | + S.css { color: S.color (f colors) } |
| 123 | + |
| 124 | +-- Paragraph |
| 125 | + |
| 126 | +type ParagraphProps = |
| 127 | + ( component :: Text |
| 128 | + , type :: Maybe TextType |
| 129 | + , content :: Array JSX |
| 130 | + ) |
| 131 | + |
| 132 | +-- | TODO: write documentation |
| 133 | +paragraph_ :: LumiComponent TextProps |
| 134 | +paragraph_ = paragraph <<< renderInnerText |
| 135 | + where |
| 136 | + renderInnerText :: PropsModifier' TextProps ParagraphProps |
| 137 | + renderInnerText f props = |
| 138 | + let |
| 139 | + props' = f $ props { content = "" } |
| 140 | + in |
| 141 | + props' |
| 142 | + { content = |
| 143 | + [ text _ |
| 144 | + { component = props'.component |
| 145 | + , type = props'.type |
| 146 | + , content = props'.content |
| 147 | + , css = \_ -> S.css { fontSize: S.inherit, lineHeight: S.inherit } |
| 148 | + } |
| 149 | + ] |
| 150 | + } |
| 151 | + |
| 152 | +-- | TODO: write documentation |
| 153 | +paragraph :: LumiComponent ParagraphProps |
| 154 | +paragraph = |
| 155 | + unsafePerformEffect $ lumiComponent "Paragraph" defaults \props -> Hooks.do |
| 156 | + theme <- useTheme |
| 157 | + pure $ |
| 158 | + E.element R.p' |
| 159 | + { children: props.content |
| 160 | + , className: props.className |
| 161 | + , css: defaultParagraphStyle theme props.type <> props.css theme |
| 162 | + } |
| 163 | + where |
| 164 | + defaults :: Record ParagraphProps |
| 165 | + defaults = |
| 166 | + { component: Text |
| 167 | + , type: Nothing |
| 168 | + , content: [] |
| 169 | + } |
| 170 | + |
| 171 | + defaultParagraphStyle :: LumiTheme -> Maybe TextType -> Style |
| 172 | + defaultParagraphStyle theme ty = |
| 173 | + S.merge |
| 174 | + [ S.css |
| 175 | + { whiteSpace: S.str "pre-wrap" |
| 176 | + , margin: S.str "0" |
| 177 | + , padding: S.str "0" |
| 178 | + } |
| 179 | + , S.toCSS (textStyle (textTheme (fromMaybe Body ty))) theme |
| 180 | + ] |
| 181 | + |
| 182 | +-- Headers |
| 183 | + |
| 184 | +data Header = Header |
| 185 | + |
| 186 | +-- Even though the header components never end up using the `type` property, we |
| 187 | +-- need it here so that text modifiers such as `strong` may also be applied to |
| 188 | +-- headers. |
| 189 | +type HeaderProps = |
| 190 | + ( component :: Header |
| 191 | + , type :: Maybe TextType |
| 192 | + , content :: String |
| 193 | + ) |
| 194 | + |
| 195 | +subsectionHeader :: LumiComponent HeaderProps |
| 196 | +subsectionHeader = mkHeaderComponent R.h4' <<< textStyle _.subsectionHeader |
| 197 | + |
| 198 | +sectionHeader :: LumiComponent HeaderProps |
| 199 | +sectionHeader = mkHeaderComponent R.h3' <<< textStyle _.sectionHeader |
| 200 | + |
| 201 | +title :: LumiComponent HeaderProps |
| 202 | +title = mkHeaderComponent R.h2' <<< textStyle _.title |
| 203 | + |
| 204 | +mainHeader :: LumiComponent HeaderProps |
| 205 | +mainHeader = mkHeaderComponent R.h1' <<< textStyle _.mainHeader |
| 206 | + |
| 207 | +-- | TODO: write documentation |
| 208 | +mkHeaderComponent |
| 209 | + :: TextElement |
| 210 | + -> LumiComponent HeaderProps |
| 211 | +mkHeaderComponent el = |
| 212 | + unsafePerformEffect $ lumiComponent "Header" defaults \props -> Hooks.do |
| 213 | + theme <- useTheme |
| 214 | + pure $ |
| 215 | + E.element el |
| 216 | + { children: [ R.text props.content ] |
| 217 | + , className: props.className |
| 218 | + , css: defaultHeaderStyle theme <> props.css theme |
| 219 | + } |
| 220 | + where |
| 221 | + defaults :: Record HeaderProps |
| 222 | + defaults = |
| 223 | + { component: Header |
| 224 | + , type: Nothing |
| 225 | + , content: "" |
| 226 | + } |
| 227 | + |
| 228 | + defaultHeaderStyle :: LumiTheme -> Style |
| 229 | + defaultHeaderStyle theme = |
| 230 | + S.merge |
| 231 | + [ S.css |
| 232 | + { fontWeight: S.str "400" |
| 233 | + , padding: S.str "0" |
| 234 | + , margin: S.str "0" |
| 235 | + } |
| 236 | + , S.toCSS (textStyle _.subsectionHeader) theme |
| 237 | + ] |
| 238 | + |
| 239 | +-- |
| 240 | + |
| 241 | +textStyle :: (forall a. TextMap a -> a) -> StyleModifier |
| 242 | +textStyle selector = |
| 243 | + S.style \theme -> |
| 244 | + S.css |
| 245 | + { fontSize: px $ textFontSize theme selector |
| 246 | + , lineHeight: px $ textLineHeight theme selector |
| 247 | + , marginBottom: px $ textMargin theme selector |
| 248 | + } |
| 249 | + |
| 250 | +px :: Int -> StyleProperty |
| 251 | +px i = S.str $ show i <> "px" |
0 commit comments