-
Notifications
You must be signed in to change notification settings - Fork 5
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
Img refactor #156
Img refactor #156
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
<script setup lang="ts"> | ||
import { useCssModule, computed } from 'vue'; | ||
import mapClasses from '../../utils/mapClasses'; | ||
import propValidator from '../../utils/propValidator'; | ||
import { CdrRadiusSoft, CdrRadiusRound, CdrRadiusSofter } from '@rei/cdr-tokens'; | ||
|
||
/** Media for capturing attention and communicating messages */ | ||
defineOptions({ | ||
name: 'CdrImg', | ||
inheritAttrs: false, | ||
}); | ||
|
||
const props = defineProps({ | ||
|
@@ -25,121 +23,77 @@ const props = defineProps({ | |
default: '', | ||
}, | ||
/** | ||
* Aspect ratio of the media container | ||
* @demoSelectMultiple false | ||
* @values auto, square, 1-2, 2-3, 3-4, 9-16, 2-1, 3-2, 4-3, 16-9 | ||
*/ | ||
* Aspect ratio of the image | ||
* @demoSelectMultiple false | ||
* @values Valid CSS aspect-ratio property values | ||
*/ | ||
ratio: { | ||
type: String, | ||
default: undefined, | ||
validator: (value: string) => ([ | ||
'auto', | ||
'square', | ||
'1-2', | ||
'2-3', | ||
'3-4', | ||
'9-16', | ||
'2-1', | ||
'3-2', | ||
'4-3', | ||
'16-9'].indexOf(value) >= 0) || false, | ||
}, | ||
/** | ||
* Requires a `ratio`. Area to crop the image overflow to. {left, center, right} {top, center, bottom} | ||
* @demoSelectMultiple false | ||
* @values left, x-center, right, top, y-center, bottom | ||
*/ | ||
crop: { | ||
* Object position of the image | ||
* @demoSelectMultiple false | ||
* @values Valid CSS object-position values | ||
*/ | ||
position: { | ||
type: String, | ||
default: undefined, | ||
}, | ||
/** | ||
* Requires a `ratio`. Scale the image to be as large as possible to fill the area (background-position: cover;) | ||
*/ | ||
cover: { | ||
type: Boolean, | ||
}, | ||
/** | ||
* Adds a custom class to the cdr-img__ratio container div | ||
*/ | ||
containerClass: String, | ||
/** | ||
* Sets a border radius to an element {square, top, right, bottom, left} | ||
* @demoSelectMultiple false | ||
* @values circle, rounded | ||
*/ | ||
radius: { | ||
* Object fit of the image | ||
* @demoSelectMultiple false | ||
* @values Valid CSS object-fit values | ||
*/ | ||
fit: { | ||
type: String, | ||
validator: (value: string) => ([ | ||
'circle', | ||
'rounded'].indexOf(value) >= 0) || false, | ||
default: undefined, | ||
}, | ||
/** | ||
* Modifies the style variant for this component. | ||
* Sets border radius | ||
* @demoSelectMultiple false | ||
* @values responsive | ||
* @values Accepts shorthand to use cedar radius tokens (soft, softer, round) or a custom value as a valid CSS border-radius values | ||
*/ | ||
modifier: { | ||
radius: { | ||
type: String, | ||
default: '', | ||
validator: (value: string) => propValidator(value, ['', 'responsive']), | ||
}, | ||
default: undefined, | ||
} | ||
}); | ||
const style = useCssModule(); | ||
const baseClass = 'cdr-image'; | ||
const ratioClass = 'cdr-image-ratio'; | ||
const coverWrapperClass = 'cdr-image-ratio__cover'; | ||
const modifierClass = computed(() => props.modifier ? `${baseClass}--${props.modifier}` : ''); | ||
const radiusClass = computed(() => props.radius ? `${baseClass}--${props.radius}` : ''); | ||
const cropObject = computed(() => ({ objectPosition: props.crop })); | ||
const ratioObject = computed(() => { | ||
let ratioPct; | ||
if (props.ratio === 'square') { | ||
ratioPct = '100%'; | ||
} else if (props.ratio === 'auto') { | ||
ratioPct = '0'; | ||
} else { | ||
if (props.ratio) { | ||
const [x, y] = props.ratio.split('-'); | ||
ratioPct = `${(+y / +x) * 100}%`; | ||
} | ||
|
||
const radiusTokens = | ||
new Map | ||
<string | undefined, typeof CdrRadiusSoft | typeof CdrRadiusSofter | typeof CdrRadiusRound> | ||
([ | ||
['soft', CdrRadiusSoft], | ||
['softer', CdrRadiusSofter], | ||
['round', CdrRadiusRound] | ||
]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This map allows for a shorthand mapping of CdrToken values. The following computed property allows the component to accept the token or a custom radius. |
||
|
||
const getRadius = computed(() => radiusTokens.get(props.radius) | ||
? `${radiusTokens.get(props.radius)}rem` | ||
: props.radius | ||
); | ||
|
||
const imageProperties = computed(() => { | ||
return { | ||
'--cdr-img-aspect-ratio': props.ratio, | ||
'--cdr-img-object-position': props.position, | ||
'--cdr-img-object-fit': props.fit, | ||
'--cdr-img-border-radius': getRadius.value, | ||
} | ||
return { '--ratio': ratioPct }; | ||
}); | ||
const cropClass = computed(() => props.crop ? `${coverWrapperClass}--crop` : ''); | ||
const coverClass = computed(() => props.cover ? `${coverWrapperClass}--cover`: ''); | ||
|
||
const baseClass = 'cdr-image'; | ||
const style = useCssModule(); | ||
|
||
</script> | ||
|
||
<template> | ||
<div | ||
v-if="ratio" | ||
:style="ratioObject" | ||
:class="[style[ratioClass], containerClass]" | ||
> | ||
<img | ||
:style="cropObject" | ||
:class="mapClasses(style, | ||
baseClass, | ||
modifierClass, | ||
radiusClass, | ||
coverWrapperClass, | ||
cropClass, | ||
coverClass, | ||
)" | ||
:src="src" | ||
:alt="alt" | ||
v-bind="$attrs" | ||
> | ||
</div> | ||
<img | ||
v-else | ||
:class="mapClasses(style, | ||
baseClass, | ||
modifierClass, | ||
radiusClass, | ||
)" | ||
:class="style[baseClass]" | ||
:style="imageProperties" | ||
:src="src" | ||
:alt="alt" | ||
v-bind="$attrs" | ||
> | ||
</template> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer necessary