swiper doesn't work on page load - swiper.js

I imported the code from https://github.com/nolimits4web/Swiper/blob/master/demos/23-thumbs-gallery.html
But the first problem I had was that the slider didn't show so I fixed that with adding min-height: 250px; to the div class .swiper-slide(this is the only thing I changed), my new problem is that the slider doesn't work.
When I resize the browser the slider suddenly works, I can't find what is causing the problem.
You can watch the slider at nielsvt.remvoo.com and then section portfolio, the slider will be visible at the bottom of the page

I had a similar issue. I simply fixed it by initializing the swiper once the page loads.
useEffect(()=> {
swiper.init()
}, [])

On the swiper section definition put the observer property true.
const swiper = new Swiper(swiperIdentifier, {
...
observer: true,
});

Related

Vuetify DarkMode colors wrong after page reload

My Vuetify App now can correctly toggle to darkmode (hooray!) however, when a full page refresh occurs, the style colors do not change (my primary color is kept from the light mode)
I can toggle to light, then back to dark, and the colors shift correctly to the dark theme's colors.
net-net
what is going on? from what i am reading, all i need to do is set this.$vuetify.theme.dark = true in the app mounted method, but it doesn't work...
UPDATE
we are using a nuxt layout, and setting the dark = true on the vuetify theme:
mounted() {
console.log('darkMode:', this.darkMode);
this.$vuetify.theme.dark = this.darkMode;
},
Did some digging, found that this is a known issue with the darkmode vuetify meta data:
https://github.com/vuetifyjs/vuetify/issues/13378
Update:
the git hub issue was closed, but the expected workaround did not work for me.
I ended up throwing a half millisecond setTimeout to reset the this.$vuetify.theme.dark = true due to the meta code changing it again to false midway through the darkmode being applied.

Insert scopedSlot into d3 svg using foreignObject

I'm creating a chart component using d3js and Vue (v2). In some parts, I want to support custom user content using scoped slots (in this case, custom tooltips)
<my-chart>
<template slot="tooltip" slot-scope="{ data }">
<span>{{ data }}</span>
</template>
</my-chart>
But I'm struggling to render this using d3js on a vuejs component render function. I'm trying to do something like:
g?.append("foreignObject").html(
this.$scopedSlots["tooltip"]({
event,
}),
);
Obviously, the html method isn't appropriated. I can't find anything like this online. All other examples use the component template to insert the foreignObject and Vue component on the SVG. Nothing using d3js
EDIT
As user I refer to developers. This code is for a lib.
Just in case anyone wants to implement something like this, I manage to resolve my issue.
Background
SVG has a concept of foreignObject which allows me to inject HTML inside an SVG. The next step is, somehow, to render the Vue component to HTML.
I'm using vue2, Vuetify, and d3js v6
Rendering the component
this.$scopedSlots["tooltip"]({
event,
}),
returns a VNode[], so using Vue.extend I create a new component, instantiate it and mount it to a div inside the foreignObject like this:
// Call the scoped slot, which returns a vnode[]
const vnodes = this.$scopedSlots["tooltip"]({
event,
});
const id = "RANDOM_GENERATED_ID";
// Append a foreignObject to an g
const fo = g?.append("foreignObject");
// Append a div to the foreignObject, which will be the vue component mount point
fo?.append("xhtml:div").attr("id", id);
// Create a new component which only render our vnodes inside a div
const component = Vue.extend({
render: (h) => {
return h(
"div",
{staticClass: "foreign-object-container"}
vnodes,
);
},
});
// Create a instance of this new component
const c = new component();
// I'm using vuetify, so I inject it here
c.$vuetify = this.$vuetify;
// Mount the component. This call will render the HTML
c.$mount("#" + id);
// Get the component rect
const bbox = c?.$el.getBoundingClientRect();
// Resize the ForeignObject to match our injected component size
return fo
?.attr("width", bbox?.width ?? 0)
.attr("height", bbox?.height ?? 0);
This successfully renders our component inside an SVG using d3js. At least it appears inside the DOM.
Problems
At this point, I faced 2 new problems.
Invalid component size
After rendering the Vue component inside the foreignObject it reported width equals 0. So, based on this I used the next styles:
.foreign-object-container {
display: inline-flex;
overflow: hidden;
}
And voilá, habemus a visible Vue component.
Scroll, ForeignObject, and the old Webkit Bug
My use case is this: The chart is responsive, so it re-renders after every container resizes (with some debounce), but to prevent deformations I set a minimum width to every element. With some screen sizes, this provokes some overflow, which inserts a scrollbar (browser behavior).
This is exactly what I want. But I'm using some Vuetify components on my scopedSlot which have a position: relative style.
Enters, an old bug on WebKit (Google Chrome, Safari, and Edge Chromium). This is better explained here and here
The solution in my case is simple. As I stated before, my foreignObject was resized to match the rendered component. So, to prevent my components to be wrongly drawn, I change my styles a little bit.
.foreign-object-container {
display: inline-flex;
overflow: hidden;
position: sticky;
position: -webkit-sticky;
}
Now, my teammates can use a generic chart with scroll support and customize some pieces of it using any Vue component (at least for now )

nuxtjs $refs scrollTop not move top

I am using nuxtjs v2.11 ,
I just want to go to testing ref but seems it not move to testing div.
<button #click="goToRefsTesting()">
go to testing dev
</button>
<div
ref="testing"
id="testing"
>
just testing
</div>
in the methods as you expect
methods: {
goToRefsTesting() {
console.log('go to ref pls')
this.$refs.testing.scrollTop = 0
},
i got the console log message, but it still not move to testing div. any thought? is it nuxt issue or what.
Your page needs to have enough content for scrollTop or scrollIntoView to be able to work.
Most likely there's nothing after the div, so the page cannot scroll.
Add some more content and you'll see the content move.
ok, i found out it is not vue/nuxt problem. the container got auto scroll properties. so that is why it is not working.thanks
This worked for me in any component without ref (but it only scrolls to top), smooth scrolling is default behavior
<button #click=scrollToTop()>Jump to top of page
methods: {
scrollToTop() {
window.scrollTo({ top: 0 });
}
}

Scrolling to v-expansion-panel opening

I'm trying to build a mobile small application using v-expansion-panels to display a list.
The idea is that when the user adds a new item in such list it will open the new panel and scroll down to such new panel.
I found a goTo() method in the $vuetify variable, unfortunatly the v-expansion-panels transition (the "opening") take some time and the goTo() won't completely scroll down because of the scrollbar height changes.
So from my understanding I need to detect the end of the transition (enter/afterEnter hook).
Per the vuetifyjs documentation, I could hope to have a "transition" property on my component. (Which isn't the case anyway). But such property is only a string so I can't hook into it.
My other idea is to, somehow, find the transition component and hook into it. Unfortunatly I have trouble understanding el/vnode and the way vuejs is building is tree like the vue-devtool show and I can't get the transition component. When debugging (in the enter hook callback of the transition) it is like the component/el/vnode has a parent but isn't the child of anybody.
Is there a way to do what I'm looking for?
Here is a jsfiddler of what I currently do: https://jsfiddle.net/kdgn80sb/
Basically it is the method I'm defining in the Vue:
methods: {
newAlarm: function() {
const newAlarmPanelIndex = this.alarms.length - 1;
this.alarms.push({title: "New line"});
this.activePanelIndex = newAlarmPanelIndex;
// TODO:
this.$vuetify.goTo(this.$refs.alarmPanels[newAlarmPanelIndex]);
}
}
Firstly you should open manually panel and then use goTo with a timeout.
It works for me, just give some time to a panel to open.
<v-expansion-panels v-model="alarmPanels">
<v-expansion-panel>
<div id="example" />
</v-expansion-panel>
</v-expansion-panels>
this.alarmPanels.push(0); // Use index of expansion-panel
setTimeout(() => {
this.$vuetify.goTo(`#${resultId}`);
}, 500);

Vue Chart.js doesn't get initialized in Vue Tab

I've got a component with two vue-tabs, with two instances of vue-chart-js in each of it. Though they get initialized without errors, a chart in the unactive tab returns nothing, when I try to extract an image from it via document.querySelector('#mySecondChart').toDataURL(). Only when I click that tab and make it active, this chart transforms into image. The chart in the default active tab transforms into image without errors. Here's the code:
<template>
<div>
<vue-tabs>
<v-tab>
<my-first-chart-component/>
</v-tab>
<v-tab>
<my-second-chart-component/>
</v-tab>
</vue-tabs>
<button #click="extractCharts">Extract charts</button>
</div>
</template>
<script>
// imports omitted
export default {
name: 'MyParentComponent',
// data and props omitted
methods: {
extractCharts() {
let charts = document.querySelectorAll('chart')
let firstChart = charts[0].toDataURL();
let secondChart = charts[1].toDataURL();
console.log(`${firstChart} \n\n\n ${secondChart}`)
}
}
}
</script>
When I click the button without going to second tab, my method outputs the DOMString of the first chart and nothing of the second chart. Only when I visit the second tab first, and then click the button, my method returns both stringified charts. Is there any way to forcefully get second chart rendered without even activating the tab containing it?
After searching through documentation, I've discovered that the reason of the problem lies within Chart.js itself -- if the parent element containing the chart has display: none or is hidden in some other ways, the canvas rendered in it gets the height and width properties equal to 0. This may be avoided if during chart instance's initialization pass to its options the following parameters:
options: {
// other options
responsive: false,
maintainAspectRatio: true
}
Then the canvas element bound to the chart instance would keep width and height properties passed to it in markup (i.e. <canvas id="myChart" width="1137" height: "447"></canvas>) and it's display property will remain with the value of block even if the parent element is hidden.
This worked for me.
Put the code inside mounted hook.
var canvas = document.getElementById('line-chart');
canvas.removeAttribute('width');
canvas.removeAttribute('height');
canvas.setAttribute('style', 'display: block; width: 100% !important; height: auto !important;');