vue-smooth-height icon indicating copy to clipboard operation
vue-smooth-height copied to clipboard

Take children with 'position: absolute;' into account

Open alxppp opened this issue 7 years ago • 2 comments

Is it possible to also account for the height of children with position: absolute? Right now their height is ignored.

alxppp avatar Apr 30 '18 13:04 alxppp

This is how I do it right now:

<template lang="pug">
div(ref="container", :style="{ height: height }")
  slot

</template>

<script>
export default {
  name: 'autoheight',
  data() {
    return {
      height: 'auto',
    };
  },
  computed: {
    computedHeight() {
      return this.height;
    },
  },
  methods: {
    maxChildHeight() {
      const children = this.$refs.container.children;
      return Math.max(Array.from(children).map(c => c.clientHeight));
    },
    updateHeight() {
      const container = window.getComputedStyle(this.$refs.container, null);
      const padding = parseFloat(container.getPropertyValue('padding-top')) +
          parseFloat(container.getPropertyValue('padding-bottom'));
      this.height = `${this.maxChildHeight() + padding}px`;
    },
  },
  mounted() {
    this.updateHeight();
    window.addEventListener('resize', this.updateHeight);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updateHeight);
  },
};
</script>

alxppp avatar Apr 30 '18 14:04 alxppp

I'm hesitant to add it as default functionality, perhaps the checks can be enabled through a boolean option? Something like

this.$registerSmoothElement({
    el: this.$refs.container,
    containsAbsolute: true
})

Another cause for concern is that your code only checks the containers direct children. It obviously suits your needs but absolutely positioned children could be at arbitrary levels of nesting. Tell me your thoughts and I can get started on this.

guanzo avatar May 01 '18 02:05 guanzo