0

Starting a large application with a handful of developers. Using webpack and bootstrap-vue. How would I default the style for <b-card> (and other components). In other words can I default:

<b-card>

to be the equivalent to:

<b-card style="max-width: 40rem;" header-bg-variant="primary" header-text-variant="white">

to make sure we keep our look consistent throughout the whole application.

3
  • 2
    Or is the answer to make my own component, my-card, that does that defaulting for them? Commented Jul 27, 2018 at 16:22
  • You can control Bootstrap's CSS via SCSS variables (need to add sass loader in your build steps). and you can also create a custom CSS file to load after bootstrap/bootstrap-vue css files that overrides class styles... .card { max-width: 40rem } Commented Jul 6, 2019 at 16:22
  • Consider making a custom component that wraps b-card, which passes the default slot to b-card and applies the preferred props/styles to b-card Commented Jul 9, 2019 at 7:33

1 Answer 1

3

You can either use SCSS/CSS overrides to control the b-card layout, or extend/wrap the component to default the props you want:

import { BCard } from 'bootstrap-vue'

export default {
  name 'MyCard',
   // Create as a functional component wrapper
  functional: true,
  // define our props (borrow from BCard)
  props: {
    ...BCard.props,
  },
  render(h, { props, data, children }) {
    return h(
      BCard,
      {
        props: {
          // Pass all props to BCard
          ...props,
          // Override the following prop defaults (if not provided)
          headerBgVariant: props.headerBgVariant || 'primary',
          headerTextVariant: props.headerBgVariant || 'white'
        },
        // Pass any additional (optional) attributes down
        attrs: data.attrs,
        // Set the style
        style: {
          // merge in any styles manually applied
          ...data.style,
          // Set the max-width if not explicitly set
          maxWidth: data.style.maxWidth || '40rem'
        }
      }
    )
  }
}

For more info on functional components and render functions, see https://v2.vuejs.org/v2/guide/render-function.html#Functional-Components

For handling more complex data merges with functional components, check out https://github.com/alexsasharegan/vue-functional-data-merge

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.