Skip to content

Commit

Permalink
Include css normalize in style output
Browse files Browse the repository at this point in the history
  • Loading branch information
opsb committed Jul 15, 2017
1 parent 0480034 commit 0c4ff2f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
13 changes: 10 additions & 3 deletions bin/style-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var pkg = require("../package.json");
var program = require("commander");
var fs = require("fs");
var chalk = require("chalk");
var requiredOptions = ["stylesheetModule", "stylesheetFunction", "output"];
var requiredOptions = ["stylesheetModule", "stylesheetFunction", "mode", "output"];
var utils = require("../js/utils");
var writeFile = utils.writeFile;
var assertKeysPresent = utils.assertKeysPresent;
Expand All @@ -14,7 +14,8 @@ var options = getOptions(process.argv, program);

styleElements({
stylesheetModule: options.stylesheetModule,
stylesheetFunction: options.stylesheetFunction
stylesheetFunction: options.stylesheetFunction,
mode: options.mode
})
.then(result => writeFile(options.output, result))
.then(() => {
Expand All @@ -32,12 +33,18 @@ function getOptions(argv, program) {
"(optional) file to write the CSS to",
"out.css"
)
.option(
"-m, --mode [layout/viewport]",
"(optional) whether to render stylesheet for 'layout' or 'viewport'",
"layout"
)
.parse(argv);

var options = {
stylesheetModule: program.args[0],
stylesheetFunction: program.args[1],
output: program.output
output: program.output,
mode: program.mode
};

assertKeysPresent(options, requiredOptions, _missingOptions => {
Expand Down
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var writeFile = utils.writeFile;
var withTmpDir = utils.withTmpDir;
var assertKeysPresent = utils.assertKeysPresent;

var requiredOptions = ["stylesheetModule", "stylesheetFunction"];
var requiredOptions = ["stylesheetModule", "stylesheetFunction", "mode"];

function generateCss(opts) {
assertKeysPresent(opts, requiredOptions, missingOptions => {
Expand All @@ -19,7 +19,8 @@ function generateCss(opts) {
var emitterWorkerFile = path.join(tmpDirPath, "style-elements-emitter.js");
var emitterTemplate = buildEmitterTemplate(
opts.stylesheetModule,
opts.stylesheetFunction
opts.stylesheetFunction,
opts.mode
);

return writeFile(emitterSourceFile, emitterTemplate)
Expand All @@ -28,32 +29,41 @@ function generateCss(opts) {
});
}

function buildEmitterTemplate(stylesheetModule, stylesheetFunction) {
function buildEmitterTemplate(stylesheetModule, stylesheetFunction, mode) {
return unindent(
`
port module StyleElementsEmitter exposing (..)
import ${stylesheetModule}
import Element
port result : String -> Cmd msg
stylesheet =
${stylesheetModule}.${stylesheetFunction}
styles =
Element.${renderFunction(mode)} ${stylesheetModule}.${stylesheetFunction}
main : Program Never () Never
main =
Platform.program
{ init = ( (), result stylesheet.css )
{ init = ( (), result styles )
, update = \\_ _ -> ( (), Cmd.none )
, subscriptions = \\_ -> Sub.none
}
`
);
}

function renderFunction(mode) {
switch (mode) {
case "viewport": return "toViewportCss";
case "layout": return "toLayoutCss";
default: throw new Error(`Invalid mode: ${mode}, must be either 'layout' or 'viewport'`);
}
}

function compile(src, options) {
return new Promise(function(resolve, reject) {
compileElm(src, options).on("close", function(exitCode) {
Expand Down
18 changes: 17 additions & 1 deletion src/Element.elm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module Element
, layout
, viewport
, toHtml
, toLayoutCss
, toViewportCss
, embedStylesheet
, Device
, classifyDevice
Expand Down Expand Up @@ -196,7 +198,7 @@ Some convient elements for working with forms.
## Advanced Rendering
@docs toHtml, embedStylesheet
@docs toHtml, embedStylesheet, toLayoutCss, toViewportCss
### Deprecated
Expand Down Expand Up @@ -1252,6 +1254,20 @@ toHtml stylesheet el =
(Render.render stylesheet el)


{-| Renders the stylesheet generated by 'layout' to css
-}
toLayoutCss : StyleSheet style variation -> String
toLayoutCss stylesheet =
Render.layoutCss stylesheet


{-| Renders the stylesheet generated by 'viewport' to css
-}
toViewportCss : StyleSheet style variation -> String
toViewportCss stylesheet =
Render.viewportCss stylesheet


{-| Embed a stylesheet.
-}
embedStylesheet : StyleSheet style variation -> Html msg
Expand Down
14 changes: 12 additions & 2 deletions src/Element/Internal/Render.elm
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,25 @@ root stylesheet elm =
(embed False stylesheet :: render stylesheet elm)


viewportCss : Internal.StyleSheet elem variation -> String
viewportCss stylesheet =
normalizeFull () ++ stylesheet.css


layoutCss : Internal.StyleSheet elem variation -> String
layoutCss stylesheet =
normalize ++ stylesheet.css


embed : Bool -> Internal.StyleSheet elem variation -> Html msg
embed full stylesheet =
Html.node "style"
[]
[ Html.text <|
if full then
normalizeFull () ++ stylesheet.css
viewportCss stylesheet
else
normalize ++ stylesheet.css
layoutCss stylesheet
]


Expand Down

0 comments on commit 0c4ff2f

Please sign in to comment.