Skip to content

Commit

Permalink
changes from pr feedback - turn css vars into style binding, add more…
Browse files Browse the repository at this point in the history
… css units, adjust regex, change code sample display
  • Loading branch information
thanksameeelian committed Jan 17, 2023
1 parent 7146ead commit 31ba8ad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 52 deletions.
52 changes: 29 additions & 23 deletions docs/pages/kimg.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
<KImg
:src="require('../assets/img_sample_for_kimg.png')"
altText="Computers are run by bees"
:height="'250px'"
:width="225"
:height="'250.2px'"
:width="225.5"
:maxHeight="600"
:minWidth="25"
/>
</DocsShow>

<pre><samp>
:src="require('../assets/img_sample_for_kimg.png')"
altText="Computers are run by bees"
:height="'250px'"
:width="225"
</samp></pre>
<DocsShowCode language="html">
<KImg
:src="require('../assets/img_sample_for_kimg.png')"
altText="Computers are run by bees"
:height="'250.2px'"
:width="225.5"
/>
</DocsShowCode>
</div>
</div>

Expand All @@ -47,12 +50,14 @@
/>
</DocsShow>
</div>
<pre><samp>
:src="require('../assets/img_sample_for_kimg.png')"
isDecorative
:height="50"
:width="50"
</samp></pre>
<DocsShowCode language="html">
<KImg
:src="require('../assets/img_sample_for_kimg.png')"
isDecorative
:height="50"
:width="50"
/>
</DocsShowCode>
</div>
</div>

Expand All @@ -65,10 +70,13 @@
altText="Computers are run by bees"
/>
</DocsShow>
<pre><samp>
:src="require('../assets/img_sample_for_kimg.png')"
altText="Computers are run by bees"
</samp></pre>
<DocsShowCode language="html">
<KImg
:src="require('../assets/img_sample_for_kimg.png')"
altText="Computers are run by bees"
/>
</DocsShowCode>

</div>
</DocsPageSection>
</DocsPageTemplate>
Expand All @@ -85,10 +93,6 @@

<style lang="scss" scoped>
pre {
margin: 0;
}
.img-example-1 {
display: flex;
margin-top: 20px;
Expand All @@ -111,3 +115,5 @@





1 change: 1 addition & 0 deletions docs/tableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export default [
path: '/kimg',
title: 'KImg',
isCode: true,
keywords: ['image', 'img'],
}),
new Page({
path: '/klabeledicon',
Expand Down
60 changes: 31 additions & 29 deletions lib/KImg.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

<div>
<img
id="kimg"
:src="src"
:alt="alternateText"
:style="cssVars"
:style="styleObject"
>
<slot></slot>
</div>
Expand Down Expand Up @@ -82,6 +81,18 @@
default: undefined,
},
},
data() {
return {
styleObject: {
height: this.imgHeight,
width: this.imgWidth,
maxHeight: this.imgMaxHeight,
minHeight: this.imgMinHeight,
maxWidth: this.imgMaxWidth,
minWidth: this.imgMinWidth,
},
};
},
computed: {
alternateText() {
return this.isDecorative ? '' : this.altText;
Expand All @@ -104,16 +115,6 @@
imgMinWidth() {
return this.validateAndFormatUnits(this.minWidth);
},
cssVars() {
return {
'--height': this.imgHeight,
'--width': this.imgWidth,
'--max-height': this.imgMaxHeight,
'--min-height': this.imgMinHeight,
'--max-width': this.imgMaxWidth,
'--min-width': this.imgMinWidth,
};
},
},
created() {
if (!this.isDecorative && !this.altText) {
Expand All @@ -123,37 +124,38 @@
methods: {
validateAndFormatUnits(propValue) {
if (propValue) {
if (Number.isInteger(propValue)) {
if (!isNaN(propValue)) {
return `${propValue}px`;
} else {
// split numbers apart from units
const [, ...arr] = propValue.match(/(\d*)([\s\S]*)/);
const validUnits = ['px', 'em', 'rem', 'vh'];
const [, ...arr] = propValue.match(/(\d*\.?\d+)([a-zA-Z | %]*)/);
const validUnits = [
'%',
'cm',
'em',
'ex',
'ch',
'in',
'lh',
'mm',
'px',
'rem',
'rlh',
'vh',
'vw',
];
// if made up of valid numbers and valid units
if (!isNaN(arr[0]) && validUnits.includes(arr[1])) {
return propValue;
}
}
}
return 'auto';
},
},
};
</script>


<style scoped>
#kimg {
height: var(--height);
width: var(--width);
max-height: var(--max-height);
min-height: var(--min-height);
max-width: var(--max-width);
min-width: var(--min-width);
}
</style>

<style scoped></style>

0 comments on commit 31ba8ad

Please sign in to comment.