Skip to content

Commit

Permalink
Rename and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Harvey committed Jul 26, 2019
1 parent 1901a7f commit b3f10f8
Show file tree
Hide file tree
Showing 19 changed files with 1,653 additions and 539 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Broadly, yes. Whether it's performant or not is yet to be seen.

### Can I try it?

Uhh. Sure.
Uhh. Sure. To see a somewhat terrible looking proof of concept, crack open the
following incantations:

`git clone https://github.com/danieljharvey/purescript-stylesheet`

`spago install`

`yarn start` and then `open index.html`. Joy!
`yarn build` and then `open index.html`. Joy!
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"license": "MIT",
"scripts": {
"pscid:build": "spago build",
"build:test": "spago bundle-app --main Testing --to index.js",
"build:test:watch": "spago bundle-app --main Testing -w --to index.js"
"build": "spago bundle-app --main Testing --to index.js",
"build:watch": "spago bundle-app --main Testing -w --to index.js"
},
"dependencies": {
"purescript": "^0.13.2"
Expand Down
20 changes: 11 additions & 9 deletions src/Main.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module PursUI (module Types, module Internal) where
module Stylesheet (module Types, module Internal) where

import PursUI.Types.Primitives (CSSSelector(..), CSSText(..), InsertMediaRule(..), InsertRule(..), MediaQueryText(..), StyleSheetId(..), UnpackedMediaRule, UnpackedRule) as Types
import PursUI.Types.VirtualStyleSheet (VirtualStyleSheet(..), getStyleSheetId) as Types
import PursUI.Types.PursUI (PursUI, createBlankStyleSheet, getCSSStyleSheet, readVirtualStyleSheet) as Types
import PursUI.Types.CSSRuleSet (CSSRule(..), CSSRuleSet(..), Props, fun, makeStyle, media, str, testProps) as Types
import PursUI.Types.StyleRuleSet (ClassRule(..), MediaRule(..), RuleType(..), StyleRuleSet(..), classRule, mediaRule, wrapInRuleSet) as Types
import Stylesheet.Types.Primitives (CSSSelector(..), CSSText(..),
InsertMediaRule(..), InsertRule(..), MediaQueryText(..), StylesheetId(..), UnpackedMediaRule, UnpackedRule) as Types
import Stylesheet.Types.VirtualStylesheet (VirtualStylesheet(..), getStylesheetId) as Types
import Stylesheet.Types.Stylesheet (Stylesheet(..), createBlankStylesheet,
getCSSStylesheet, readVirtualStylesheet) as Types
import Stylesheet.Types.CSSRuleSet (CSSRule(..), CSSRuleSet(..), fun, media, str) as Types
import Stylesheet.Types.StyleRuleSet (ClassRule(..), MediaRule(..), RuleType(..), StyleRuleSet(..), classRule, mediaRule, wrapInRuleSet) as Types

import PursUI.DomActions (createAndReturnCSSMediaRule, createStyleTag, findMediaQueryByQuery, findRuleBySelector, getFilteredRuleList, getMediaRuleMediaText, getStyleRuleDeclarationText, getStyleRuleSelectorText, getUnpackedMediaRules, getUnpackedStyleRules, insertRecursive, insertRule, unpackMediaRule, unpackRule) as Internal
import PursUI.StyleLens (createHashedInsertRule, getClasses, keep, processStyle) as Internal
import PursUI.AddStyle (addStyle) as Internal
import Stylesheet.Internal.DomActions (createAndReturnCSSMediaRule, createStyleTag, findMediaQueryByQuery, findRuleBySelector, getFilteredRuleList, getMediaRuleMediaText, getStyleRuleDeclarationText, getStyleRuleSelectorText, getUnpackedMediaRules, getUnpackedStyleRules, insertRecursive, insertRule, unpackMediaRule, unpackRule) as Internal
import Stylesheet.Internal.ProcessStyles (createHashedInsertRule, getClasses, keep, processStyle) as Internal
import Stylesheet.Internal.AddStyle (addStyle) as Internal
30 changes: 0 additions & 30 deletions src/PursUI/AddStyle.purs

This file was deleted.

61 changes: 0 additions & 61 deletions src/PursUI/Types/CSSRuleSet.purs

This file was deleted.

54 changes: 0 additions & 54 deletions src/PursUI/Types/PursUI.purs

This file was deleted.

51 changes: 0 additions & 51 deletions src/PursUI/Types/VirtualStyleSheet.purs

This file was deleted.

30 changes: 30 additions & 0 deletions src/Stylesheet/Internal/AddStyle.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Stylesheet.Internal.AddStyle where

import Prelude (discard, pure)
import Effect (Effect)
import Data.Foldable (foldMap)

import Stylesheet.Types.Stylesheet (Stylesheet(..), addVirtualStylesheet)
import Stylesheet.Types.CSSRuleSet (CSSRuleSet)
import Stylesheet.Types.Primitives (CSSSelector)
import Stylesheet.Types.VirtualStylesheet

import Stylesheet.Internal.ProcessStyles (getClasses, keep, processStyle)

-- | This is the top level big dog of a function, as such
-- | We provide the big Stylesheet doing-things-lump
-- | a function from props -> Styles
-- | some props
-- | and we get back a bunch of CSSSelectors (classes)
addStyle
:: forall p props
. Stylesheet p
-> CSSRuleSet props
-> props
-> Effect (Array CSSSelector)
addStyle stylesheet styleRule props = do
let rules = keep (processStyle styleRule props)
virtualStylesheet = foldMap (fromItem) rules
classes = foldMap (getClasses) rules
addVirtualStylesheet stylesheet virtualStylesheet
pure classes
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module PursUI.DomActions where
module Stylesheet.Internal.DomActions where

import Prelude
import Data.Maybe (Maybe(..))
import Data.Traversable (find, traverse, traverse_)
import Effect (Effect)
import Effect.Uncurried (runEffectFn1)
import PursUI.Types.Primitives (CSSSelector(..), CSSText(..), InsertMediaRule(..), InsertRule(..), MediaQueryText(..), StyleSheetId(..), UnpackedMediaRule, UnpackedRule)
import Stylesheet.Types.Primitives
import CSSOM.Main (class HasRuleList, CSSMediaRule, CSSRuleList, CSSRules, CSSStyleRule, CSSStyleSheet, IndexedRule, createStyleTagJS, deleteRule', getFilteredRuleListJS, getMediaRuleMediaTextJS, getRuleList', getStyleRuleDeclarationTextJS, getStyleRuleSelectorTextJS, insertRule')

-- this takes all the raw calls and builds up a few actually useful functions
-- for CSSOM manipulation

createStyleTag
:: StyleSheetId
:: StylesheetId
-> Effect CSSStyleSheet
createStyleTag (StyleSheetId s)
createStyleTag (StylesheetId s)
= runEffectFn1 createStyleTagJS s

-- | Get all the rules of a stylesheet, split by type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module PursUI.StyleLens where
module Stylesheet.Internal.ProcessStyles where

import Prelude (flap, map, pure, show, ($), (<<<), (<>))
import PursUI.Types.CSSRuleSet (CSSRule(..), CSSRuleSet(..))
import PursUI.Types.StyleRuleSet
import PursUI.Types.Primitives (CSSSelector(..), CSSText, InsertMediaRule(..), InsertRule(..))
import Stylesheet.Types.CSSRuleSet (CSSRule(..), CSSRuleSet(..))
import Stylesheet.Types.StyleRuleSet
import Stylesheet.Types.Primitives (CSSSelector(..), CSSText, InsertMediaRule(..), InsertRule(..))
import Data.Array (concatMap)
import Data.Foldable (foldr)
import Data.Hashable (hash)
Expand All @@ -26,9 +26,7 @@ processStyle ruleSet props
Lens f -> classRule (f props')
MediaQuery s as -> mediaRule s (process as)

-- for now we'll discard media query ones
-- and just output the basic bullshit

-- | This creates a unique hash of the CSS that we'll use to make the classname
createHashedInsertRule :: CSSText -> InsertRule
createHashedInsertRule css
= InsertRule selector css
Expand All @@ -39,6 +37,8 @@ createHashedInsertRule css
selector
= CSSClassSelector <<< appendPrefix <<< show <<< hash $ css

-- | This commits the rendered StyleRuleSet into InsertMediaRules that we can
-- | store and apply to the CSSOM
keep :: StyleRuleSet -> Array InsertMediaRule
keep (StyleRuleSet rules)
= foldr fold [] rules
Expand All @@ -53,6 +53,8 @@ keep (StyleRuleSet rules)
MediaType (MediaRule query rules')
-> pure (InsertMediaQuery query (keep (StyleRuleSet rules')))

-- | Get the generated CSS classes from the CSS. These are generated from
-- | hashes of the content
getClasses :: InsertMediaRule -> Array CSSSelector
getClasses (InsertStyleRule (InsertRule selector _))
= pure selector
Expand Down
27 changes: 27 additions & 0 deletions src/Stylesheet/Types/CSSRuleSet.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Stylesheet.Types.CSSRuleSet where

import Prelude (class Monoid, class Semigroup, map, pure, ($), (<<<))
import Stylesheet.Types.Primitives

-- | CSSRuleSet is what we build up for a type
newtype CSSRuleSet p
= CSSRuleSet (Array (CSSRule p))

derive newtype instance semigroupCSSRuleSet :: Semigroup (CSSRuleSet p)
derive newtype instance monoidCSSRuleSet :: Monoid (CSSRuleSet p)

data CSSRule p
= Const CSSText
| Lens (p -> CSSText)
| MediaQuery MediaQueryText (CSSRuleSet p)

str :: forall props. String -> CSSRuleSet props
str = CSSRuleSet <<< pure <<< Const <<< CSSText

fun :: forall props. (props -> String) -> CSSRuleSet props
fun = CSSRuleSet <<< pure <<< Lens <<< (map CSSText)

media :: forall props. String -> CSSRuleSet props -> CSSRuleSet props
media queryStr subStyle
= CSSRuleSet <<< pure $ MediaQuery (MediaQueryText queryStr) subStyle

Loading

0 comments on commit b3f10f8

Please sign in to comment.