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

Add rig.simple, for breaks on single element #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,33 @@ rig = (block, config, graph, params, items) ->

return mediaQueries.join '\n\n'

rig.simple = (block, config, selector, graph, params, breaks, dimension) ->

if not graph
graph = 'passthrough'
if not params
params = {}
if not dimension
dimension = 'width'
if not breaks
breaks = [ 400, 800, 1600 ]
Copy link
Contributor

Choose a reason for hiding this comment

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

My preference would be to omit default values and encourage usage to be very intentional, especially the breakpoints which are arbitrary.

For common use cases I'd rather pass these as params inside of a dedicated method. Something like:

Rig.resize = (block, config, selector, breakpoints, sizes) ->
  return Rig.simple block, config, selector, 'passthrough', {}, breakpoints, sizes

Copy link
Contributor

Choose a reason for hiding this comment

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

or rather, something like:

Rig.resize = (block, config, options) ->
  return Rig.simple block, config, 'passthrough', {}, options

Rig.resize block, config,
  selector: '#media'
  breakpoints: [504, 1008]
  widths: [200, 400, 800]


items = []
for idx in [0...breaks.length]
br = breaks[idx]
size = if idx == breaks.length-1 then br else br-1
query = if idx == 0
"(max-#{dimension}: #{br-1}px)"
else if idx == breaks.length-1
"(min-#{dimension}: #{breaks[idx-1]}px)"
else
"(min-#{dimension}: #{breaks[idx-1]}px) and (max-#{dimension}: #{br-1}px)"
item =
selector: selector
query: query
item[dimension] = size
items.push item

return rig block, config, graph, params, items

module.exports = rig
38 changes: 38 additions & 0 deletions spec/rig.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,41 @@ describe 'Responsive image generator', ->
}
}
"""

describe 'using simple breaks on single element', ->

it 'should generate equivalent media query to full notation', ->

fakeBlock =
type: 'media'
cover:
src: 'https://a.com/b.png'
width: 1600
height: 900

config =
server: 'https://imgflo.herokuapp.com/'
key: process.env.IMGFLO_KEY
secret: process.env.IMGFLO_SECRET

params =
'std-dev-x': 15
'std-dev-y': 15
selector = '#my-favorite-id'

ref = rig fakeBlock, config, 'gaussianblur', params, [
query: '(max-width: 399px)'
selector: selector
width: 399
,
query: '(min-width: 400px) and (max-width: 589px)'
selector: selector
width: 589
,
query: '(min-width: 590px)'
selector: selector
width: 1368
]

css = rig.simple fakeBlock, config, selector, 'gaussianblur', params, [400, 590, 1368], 'width'
expect(css).to.equal ref