Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(theme): SASS-based themes #322

Merged
merged 10 commits into from
Oct 28, 2015
2 changes: 1 addition & 1 deletion example/default.css
10,336 changes: 6,659 additions & 3,677 deletions npm-shrinkwrap.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Algolia <support@algolia.com>",
"scripts": {
"build": "./scripts/build.sh",
"dev": "npm install && webpack-dev-server --config webpack.example.config.js --hot --inline --no-info",
"dev": "./scripts/dev.sh",
"doctoc": "doctoc --maxlevel 3 README.md",
"gh-pages": "./scripts/gh-pages.sh",
"lint": "eslint . --quiet --no-color && stylelint './themes/**/*.css'",
Expand Down Expand Up @@ -47,6 +47,7 @@
"mocha-jsdom": "^1.0.0",
"mversion": "^1.10.1",
"nd": "^1.2.0",
"node-sass": "^3.3.3",
"npm-shrinkwrap": "^5.4.1",
"pretty-bytes": "^2.0.1",
"raw-loader": "^0.5.1",
Expand Down
13 changes: 10 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

set -ev # exit when error

ROOT=`dirname "$0"`/..

mkdir -p dist/themes

license="/*! instantsearch.js ${VERSION:-UNRELEASED} | © Algolia SAS | github.com/algolia/instantsearch.js */"
license="/*! instantsearch.js ${VERSION:-UNRELEASED} | © Algolia Inc. and other contributors; Licensed MIT | github.com/algolia/instantsearch.js */"

bundle='instantsearch'

webpack

cp themes/default.css dist/themes/default.css
cleancss dist/themes/default.css > dist/themes/default.min.css
for source in "$ROOT"/themes/[^_]*.sass; do
base=`basename "$source" .sass`
echo "$license" > dist/themes/$base.css
echo >> dist/themes/$base.css
node-sass "$source" >> dist/themes/$base.css
cleancss dist/themes/$base.css > dist/themes/$base.min.css
done

printf "$license" | cat - dist/${bundle}.js > /tmp/out && mv /tmp/out dist/${bundle}.js
cd dist
Expand Down
8 changes: 8 additions & 0 deletions scripts/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -ev # exit when error

npm install
node-sass -o ./dist/themes/ --watch ./themes &
webpack-dev-server --config webpack.example.config.js --hot --inline --no-info &
wait
20 changes: 20 additions & 0 deletions themes/_base.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@mixin component($component)
.ais-#{$component}
@content

@mixin modifier($modifier)
&--#{$modifier}
@content

@mixin element($element)
&__#{$element}
@content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you use that?

Hmm, the goal of BEM is to not have to do any nesting. Sass can let you easily do a lot of nesting, but we should actually never nest more than 2 or 3 levels deep.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, the good thing it that is doesn't nest anything :) The nesting is just use to build the class names from the parent using that &-- and &__ syntax.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. I did not know that syntax.

I'm not sure the end result in Sass will be any more readable than plain CSS, though :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the end result in Sass will be any more readable than plain CSS, though :)

For me, no-brainer :)

/* REFINEMENT LIST */
.ais-refinement-list {
}
.ais-refinement-list--header {
  font-size: 1.2em;
}
.ais-refinement-list--body {
}
.ais-refinement-list--list {
}
.ais-refinement-list--item {
}
.ais-refinement-list--item__active {
  font-weight: bold;
}
.ais-refinement-list--label {
}
.ais-refinement-list--checkbox {
}
.ais-refinement-list--count {
  float: right;
  display: inline-block;
  background: black;
  color: white;
}
.ais-refinement-list--footer {
}

VS

// REFINEMENT LIST
+component(refinement-list)
  +modifier(header)
    font-size: 1.2em
  +modifier(body)
  +modifier(list)
  +modifier(item)
    +element(active)
       font-weight: bold
  +modifier(label)
  +modifier(checkbox)
  +modifier(count)
    float: right
    display: inline-block
    background: black
    color: white
  +modifier(footer)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my point exactly. I find the first one easier to read :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can use the other mixin:

// REFINEMENT LIST
+bem(refinement-list, header)
  font-size: 1.2em
+bem(refinement-list, body)
+bem(refinement-list, list)
+bem(refinement-list, header)
+bem(refinement-list, item)
+bem(refinement-list, item, active)
     font-weight: bold
+bem(refinement-list, label)
+bem(refinement-list, checkbox)
+bem(refinement-list, count)
    float: right
    display: inline-block
    background: black
    color: white
+bem(refinement-list, footer)


@mixin bem($component, $modifier, $element: '')
+component($component)
+modifier($modifier)
@if $element != ''
+element($element)
@content
@else
@content
262 changes: 0 additions & 262 deletions themes/default.css

This file was deleted.

13 changes: 13 additions & 0 deletions themes/default.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@import "base"

@import "default/search-box"
@import "default/stats"
@import "default/index-selector"
@import "default/hits"
@import "default/pagination"
@import "default/refinement-list"
@import "default/menu"
@import "default/toggle"
@import "default/hierarchical-menu"
@import "default/range-slider"
@import "default/price-ranges"
14 changes: 14 additions & 0 deletions themes/default/_hierarchical-menu.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@import '../base'
@import 'variables'

+component(hierarchical-menu)
+modifier(header)
+modifier(body)
+modifier(list)
+element(lvl0)
+element(lvl1)
+modifier(item)
+element(active)
+modifier(link)
+modifier(count)
+modifier(footer)
6 changes: 6 additions & 0 deletions themes/default/_hits.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import '../base'
@import 'variables'

+component(hits)
+element(empty)
+modifier(item)
5 changes: 5 additions & 0 deletions themes/default/_index-selector.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '../base'
@import 'variables'

+component(index-selector)
+modifier(item)
12 changes: 12 additions & 0 deletions themes/default/_menu.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import '../base'
@import 'variables'

+component(menu)
+modifier(header)
+modifier(body)
+modifier(list)
+modifier(item)
+element(active)
+modifier(link)
+modifier(count)
+modifier(footer)
Loading