Skip to content

feat(card): support left and right image placement #1981

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

Merged
merged 14 commits into from Oct 26, 2018
Merged
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
9 changes: 9 additions & 0 deletions docs/assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ pre.editable.live:after {
min-height: 10rem;
background-color: rgba(255, 0, 0, .1);
}

.card-img-left {
border-top-left-radius: calc(0.25rem - 1px);
border-bottom-left-radius: calc(0.25rem - 1px);
}
.card-img-right {
border-top-right-radius: calc(0.25rem - 1px);
border-bottom-right-radius: calc(0.25rem - 1px);
}
37 changes: 33 additions & 4 deletions src/components/card/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ card is changed.

```html
<div>
<h4>Top and Bottom</h4>
<b-card-group deck>
<b-card img-src="https://placekitten.com/1000/300"
img-alt="Card image"
Expand All @@ -114,10 +115,38 @@ card is changed.
<p class="card-text">
Some quick example text to build on the card and make up the bulk of the card's content.
</p>
</b-card>
</b-card-group>
</b-card>
</b-card-group>
<br>
<h4>Left and Right (or Start and End)</h4>
<b-card img-src="https://placekitten.com/300/300"
img-alt="Card image"
img-left
class="mb-3">
<p class="card-text">
Add to your css: <br>
<code>
.card-img-left { <br>
&nbsp;&nbsp;border-top-left-radius: calc(0.25rem - 1px); <br>
&nbsp;&nbsp;border-bottom-left-radius: calc(0.25rem - 1px); <br>
}
</code>
</p>
</b-card>
<b-card img-src="https://placekitten.com/300/300"
img-alt="Card image"
img-right>
<p class="card-text">
Add to your css: <br>
<code>
.card-img-right { <br>
&nbsp;&nbsp;border-top-right-radius: calc(0.25rem - 1px); <br>
&nbsp;&nbsp;border-bottom-right-radius: calc(0.25rem - 1px); <br>
}
</code>
</p>
</b-card>
</div>

<!-- card-img-1.vue -->
```

Expand All @@ -126,7 +155,7 @@ Place the image in the background of the card by setting the boolean prop `overl
```html
<div>
<b-card overlay
img-src="https://picsum.photos/900/250/?image=36"
img-src="https://picsum.photos/900/250/?image=3"
img-alt="Card Image"
text-variant="white"
title="Image Overlay"
Expand Down
38 changes: 34 additions & 4 deletions src/components/card/card-img.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mergeData } from 'vue-functional-data-merge'
import {mergeData} from 'vue-functional-data-merge'

export const props = {
src: {
Expand All @@ -18,6 +18,32 @@ export const props = {
type: Boolean,
default: false
},
left: {
type: Boolean,
default: false
},
start: {
type: Boolean,
default: false
// alias of 'left'
},
right: {
type: Boolean,
default: false
},
end: {
type: Boolean,
default: false
// alias of 'right'
},
height: {
type: String,
default: null
},
width: {
type: String,
default: null
},
fluid: {
type: Boolean,
default: false
Expand All @@ -27,20 +53,24 @@ export const props = {
export default {
functional: true,
props,
render (h, { props, data, slots }) {
render (h, {props, data, slots}) {
let staticClass = 'card-img'
if (props.top) {
staticClass += '-top'
} else if (props.right || props.end) {
staticClass += '-right'
} else if (props.bottom) {
staticClass += '-bottom'
} else if (props.left || props.start) {
staticClass += '-left'
}

return h(
'img',
mergeData(data, {
staticClass,
class: { 'img-fluid': props.fluid },
attrs: { src: props.src, alt: props.alt }
class: {'img-fluid': props.fluid},
attrs: {src: props.src, alt: props.alt, height: props.height, width: props.width}
Copy link
Member

Choose a reason for hiding this comment

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

Will this always add a width and height attributes to the rendered element?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it is null it is omitted

})
)
}
Expand Down
17 changes: 10 additions & 7 deletions src/components/card/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default {
// The order of the conditionals matter.
// We are building the component markup in order.
let childNodes = []
let staticClass = 'card'
const $slots = slots()
let img = props.imgSrc
? h(CardImg, {
Expand All @@ -50,13 +51,15 @@ export default {
)
})
: null

if (img) {
// Above the header placement.
if (props.imgTop || !props.imgBottom) {
childNodes.push(img)
if (img && !props.imgBottom) {
childNodes.push(img)
if (props.imgLeft || props.imgStart) {
staticClass += ' flex-row'
} else if (props.imgRight || props.imgEnd) {
staticClass += ' flex-row-reverse'
}
}

if (props.header || $slots.header) {
childNodes.push(
h(CardHeader, { props: pluckProps(headerProps, props) }, $slots.header)
Expand All @@ -74,15 +77,15 @@ export default {
h(CardFooter, { props: pluckProps(footerProps, props) }, $slots.footer)
)
}

if (img && props.imgBottom) {
// Below the footer placement.
childNodes.push(img)
}

return h(
props.tag,
mergeData(data, {
staticClass: 'card',
staticClass: staticClass,
class: {
[`text-${props.align}`]: Boolean(props.align),
[`bg-${props.bgVariant}`]: Boolean(props.bgVariant),
Expand Down
10 changes: 10 additions & 0 deletions src/components/card/card.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('card', async () => {
})
})

it('should add flex-row to a img-left or img-right image', async () => {
const { app } = window
const node = app.$refs['img_card']
const childNodes = [...node.childNodes]
const imgEl = childNodes.find(el => el.tagName && el.tagName === 'IMG')

expect(imgEl).toBeDefined()
expect(imgEl.classList).toEqual(expect.objectContaining(/^flex-row/))
})

it("should use the 'tag' for element tag", async () => {
const { app: { $refs } } = window
const $titleCard = $refs.card_group.querySelector('#title-tag-test')
Expand Down