Skip to content

Commit

Permalink
fix(image): programmatically generate image classes and adds new rati…
Browse files Browse the repository at this point in the history
…os (#29)

* refactor(image): programatically generate image classes

* docs: add new aspec ratios to docs
  • Loading branch information
adamraider authored Mar 13, 2019
1 parent 17ea03d commit 5f96f76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
12 changes: 7 additions & 5 deletions docs/components/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ title: Image

Use these modifiers with `.ray-image` or `.ray-bg` classes.

| Selector | Description |
| ----------------------- | --------------------------------- |
| .ray-{image\|bg}--3by4 | Selector for 3 by 4 aspect ratio |
| .ray-{image\|bg}--16by9 | Selector for 16 by 9 aspect ratio |
| .ray-{image\|bg}--4by3 | Selector for 4 By 3 aspect ratio |
| Selector | Description |
| ------------------------ | -------------------------------------- |
| .ray-{image\|bg}--3by4 | Selector for 3:4 aspect ratio |
| .ray-{image\|bg}--16by9 | Selector for 16:9 aspect ratio |
| .ray-{image\|bg}--4by3 | Selector for 4:3 aspect ratio |
| .ray-{image\|bg}--1by1 | Selector for 1:1 aspect ratio |
| .ray-{image\|bg}--cinema | Selector for 2.4:1 cinema aspect ratio |
40 changes: 16 additions & 24 deletions packages/core/src/components/image/_image.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
$ray-image-ratio-map: (
4by3: calc(100% / (4 / 3)),
3by4: calc(100% / (3 / 4)),
1by1: calc(100% / 1),
16by9: calc(100% / (16 / 9)),
cinema: calc(100% / 2.4)
);

@mixin image {
display: block;
width: 100%;
Expand Down Expand Up @@ -37,18 +45,10 @@
.#{$ray-class-prefix}image {
@include image;

&--16by9 {
padding-bottom: calc(100% / (16 / 9));
}

&--3by4 {
@include image;
padding-bottom: calc(100% / (3 / 4));
}

&--4by3 {
@include image;
padding-bottom: calc(100% / (4 / 3));
@each $ratio, $value in $ray-image-ratio-map {
&--#{$ratio} {
padding-bottom: $value;
}
}
}

Expand All @@ -67,17 +67,9 @@
.#{$ray-class-prefix}bg {
@include background;

&--16by9 {
padding-bottom: calc(100% / (16 / 9));
}

&--3by4 {
@include background;
padding-bottom: calc(100% / (3 / 4));
}

&--4by3 {
@include background;
padding-bottom: calc(100% / (4 / 3));
@each $ratio, $value in $ray-image-ratio-map {
&--#{$ratio} {
padding-bottom: $value;
}
}
}
32 changes: 26 additions & 6 deletions packages/core/stories/image.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import { storiesOf } from '@storybook/react';
import getPlaceholderURL from './util/placeholder';

storiesOf('Image', module)
.add('16/9 img', () => (
.add('16:9 img', () => (
<div style={{ maxWidth: '500px' }}>
<div className="ray-image ray-image--16by9">
<img src={getPlaceholderURL('800x800')} />
</div>
</div>
))
.add('3/4 img', () => (
.add('3:4 img', () => (
<div style={{ maxWidth: '400px' }}>
<div className="ray-image ray-image--3by4">
<img src={getPlaceholderURL('800x1200')} />
</div>
</div>
))
.add('4/3 img', () => (
.add('4:3 img', () => (
<div style={{ maxWidth: '400px' }}>
<div className="ray-image ray-image--4by3">
<img src={getPlaceholderURL('1200x800')} />
</div>
</div>
))
.add('16/9 background', () => (
.add('16:9 background', () => (
<div style={{ maxWidth: '500px' }}>
<div
className="ray-bg ray-bg--16by9"
Expand All @@ -35,7 +35,7 @@ storiesOf('Image', module)
/>
</div>
))
.add('3/4 background', () => (
.add('3:4 background', () => (
<div style={{ maxWidth: '400px' }}>
<div
className="ray-bg ray-bg--3by4"
Expand All @@ -45,7 +45,7 @@ storiesOf('Image', module)
/>
</div>
))
.add('4/3 background', () => (
.add('4:3 background', () => (
<div style={{ maxWidth: '400px' }}>
<div
className="ray-bg ray-bg--4by3"
Expand All @@ -55,6 +55,26 @@ storiesOf('Image', module)
/>
</div>
))
.add('1:1 background', () => (
<div style={{ maxWidth: '400px' }}>
<div
className="ray-bg ray-bg--1by1"
style={{
backgroundImage: `url(${getPlaceholderURL('1200x800')})`
}}
/>
</div>
))
.add('Cinema background', () => (
<div style={{ maxWidth: '400px' }}>
<div
className="ray-bg ray-bg--cinema"
style={{
backgroundImage: `url(${getPlaceholderURL('1200x800')})`
}}
/>
</div>
))
.add('img with caption', () => (
<div style={{ maxWidth: '500px' }}>
<div className="ray-image ray-image--16by9">
Expand Down

0 comments on commit 5f96f76

Please sign in to comment.