How to use Font-Awesome SVG with Vuetify and Nuxt.js - font-awesome

I am trying to use the Font-Awesome SVG icons with Vuetify. But I don't manage to display icons.
I installed the necessary packages with:
npm install #fortawesome/fontawesome-svg-core #fortawesome/vue-fontawesome #fortawesome/free-solid-svg-icons -D
And the Vuetify plugin file looks like this:
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
import colors from 'vuetify/es5/util/colors'
import { library } from '#fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '#fortawesome/vue-fontawesome'
import { fas } from '#fortawesome/free-solid-svg-icons'
Vue.component('font-awesome-icon', FontAwesomeIcon)
library.add(fas)
Vue.use(Vuetify, {
iconfont: 'faSvg',
theme: {
primary: colors.blue.darken2,
accent: colors.grey.darken3,
secondary: colors.amber.darken3,
info: colors.teal.lighten1,
warning: colors.amber.base,
error: colors.deepOrange.accent4,
success: colors.green.accent3
}
})
Trying to show a icon:
<v-icon color="white" round>fa-times</v-icon>

It's really simple. Nuxt js + Vuetify 2:
npm i -D #fortawesome/fontawesome-free
U can check your package.json file:
"devDependencies": {
"#fortawesome/fontawesome-free": "^5.11.2"
}
In nuxt.config.js file:
vuetify: {
defaultAssets: {icons: 'fa'}
}
That's all! U can use:
<v-icon>fa-times</v-icon>

You should use the icons: { iconfont: 'faSvg' } syntax.
If anyone is using the #nuxtjs/vuetify module, you can configure the iconset in vuetify to use faSvg and then use the fontawesome-module for SVG icons. At this time of writing, vuetify is using the free solid icons in most of its components, so including all solid icons should be enough. You can configure this in the fontawesome-module so:
...otherNuxtConfigStuff,
buildModules: [
...otherBuildModules,
// https://github.com/nuxt-community/fontawesome-module
['#nuxtjs/fontawesome', {
icons: {
solid: true,
// here you can include other icons you need
brands: [
'faYoutube',
'faVimeo',
'faVimeoV'
]
}
}],
// https://github.com/nuxt-community/vuetify-module
['#nuxtjs/vuetify', {
...otherVuetifyConfigStuff,
icons: {
iconfont: 'faSvg'
}
}]
]
}
Make sure you don't change the component option in the fontawesome configuration because vuetify uses the default component name font-awesome-icon.

Related

storybook darkmode not playing nice

So I am using storybook for my svelte + tailwind app, and I am now trying to make sure that I can toggle darkmode.
So for my tailwind.config.js I added this
module.exports = {
darkMode: "class",
and I installed this addon to storybook
https://github.com/hipstersmoothie/storybook-dark-mode
with this config .storybook/preview.js
export const parameters = {
darkMode: {
darkClass: "dark",
stylePreview: false,
},
And by looking in the DOM of the storybook iframe I can see that "dark" is applied to the body.
But when I create a component with this HTML
<div class="inline">
<div class="w-8 h-8 bg-blue-500 dark:bg-green-500" />
</div>
the box is always blue.
So I thought maybe purgecss was removing it, and so I added safelist: ["dark"] to it's options but without any luck.
So to make things more complicated I tested this component
<div class="inline">
<div class="w-8 h-8 bg-blue-500 dark:bg-green-500" />
</div>
<div class="inline dark">
<div class="w-8 h-8 bg-blue-500 dark:bg-green-500" />
</div>
and to my surprise, one of the boxes turned green.
Honestly, I am not entirely sure if this is because of svelte, storybook, tailwind, or the darkmode storybook plugin.
But I would really appreciate help if anyone has seen something similar
You could try ignoring purgecss when watching for storybook.
I am not sure about your exact setup but in my case I added a conditional in postcss.config.js for storybook to work correctly:
const isProduction =
!process.env.ROLLUP_WATCH &&
!process.env.LIVERELOAD &&
process.env.NODE_ENV !== 'development'
module.exports = {
plugins: [
require('tailwindcss'),
...(isProduction ? [purgecss] : [])
]
};
My .storybook/preview.js contains the following:
export const parameters = {
darkMode: {
stylePreview: true,
darkClass: 'dark',
lightClass: 'light',
}
}
The only thing which still doesn't work after this is the white text in dark mode, so I had to add .dark { color: white; } to my css.
I had this issue as well but it was because I defined a prefix of vc- in my tailwind.config.js file.
When I configured the addon https://github.com/hipstersmoothie/storybook-dark-mode, I used the class dark not vc-dark in .storybook/preview.js:
export const parameters = {
darkMode: {
dark: { ...themes.dark },
light: { ...themes.light },
darkClass: 'dark',
stylePreview: true
}
}
should be
export const parameters = {
darkMode: {
dark: { ...themes.dark },
light: { ...themes.light },
darkClass: 'vc-dark',
stylePreview: true
}
}
Not sure if you, (OP), have a prefix defined in your tailwind.config.js file but it's something to watch out for, if others are having the same issue.
Even with the prefix, you can still use the dark variant normally, just don't forget to use the prefix when referencing class names after the variant:
<div class="vc-bg-blue-500 dark:vc-bg-green-500" />
This happens because components are rendered inside of an iframe and storybook-dark-mode (SDM) only sets the class to "dark" on the body of the main document.
I verified this by inspecting and adding it manually. Assuming that you have darkMode: 'class' set in your tailwind config, you should see it work as soon as you set <body class="dark"> inside that iframe. This is why when OP wrapped it in a parent with "dark", it worked for that instance only.
First attempt
The question to me is how to get that class applied to the body of the iframe as well? Reading SDM docs, it implies that it would apply it to the app as well as the preview window, but that doesn't seem to happen for me.
Interestingly, there is an add-on called storybook-tailwind-dark-mode (STDM) which adds "dark" to the <html> of the iframed document, so that's good; but it's a separate button. You can have your components render in dark or light mode independent of dark mode on the app itself.
This is currently the only way it's working for me and I'd like to see/make a fork off one of these where it does both at once.
FWIW, without Tailwind, we were using a ThemeProvider from StyledComponents that leveraged useDarkMode() from SDM to then pass that down to all the StyledComponents (which we're migrating away from in favor of Tailwind). It would be nice to leverage that somehow.
Final answer
That previous paragraph gave me some inspiration. Storybook has decorators, which are basically functions that return components. We can wrap our stories with some HTML and give it a class based on useDarkMode().
Below is more or less what I ended up using and it's working great. One button to control dark mode, no need for an additional tailwind-specific dependency, and I'm still able to use my StyledComponent theming for the components that haven't been migrated yet.
.storybook/theme.js
import React from 'react'
import { ThemeProvider } from 'styled-components'
import { themeV2, GlobalStylesV2 } from 'propritary-design-library'
import { useDarkMode } from 'storybook-dark-mode'
import '../src/index.css'
const ThemeDecorator = storyFn => {
const mode = useDarkMode() ? 'dark' : 'light'
return (
<ThemeProvider theme={themeV2(mode)}>
<section className={mode}>
<GlobalStylesV2 />
{storyFn()}
</section>
</ThemeProvider>
)
}
export default ThemeDecorator
.storybook/preview.js
import { addDecorator } from '#storybook/react'
import ThemeDecorator from './theme'
addDecorator(ThemeDecorator)
export const parameters = {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}

why the html content not shown out when running

Now i use visual studio code to do my project. I can build my code without error, but when running, it no show out the content for html file, only have show css like header abd footer. i have click button on header but cannot go to other page.Here is the sample code
code in index.html
<nav>
List
New student
Student feedback
</nav>
Vue router
const router = new VueRouter({
routes: [
{ path: '/home', component: load('home') },
{ path: '/insert', component: load('insert') },
{ path: '/update/:id', component: load('update') },
{ path: '/feedback', component: load('feedback') },
{ path: '*', redirect: '/home' }
]
});
File name and type: _home.html, _insert.html, _update.html, _feedback.html
Can help me see the problem, thank you
I don't think you should edit directly to index.html as Vue is Single Page Application (SPA) framework. Instead, you should use Vue Component for each page.
This video might help you to figure out how to use Vue and Vue Router properly: https://youtu.be/nnVVOe7qdeQ
Edit:
For sake of clarity, Let me build simplified diagram of Vue project for you.
First of all, make sure you create the project via vue cli. It guides you to build your new vue project better.
Let's say we have 3 pages:
Home
About
Another
Each page has its own CSS, HTML (we call it template), and JavaScript in one file, the .vue file. To connect them, we need a first entrance, main.js. Inside of it, we can configure the router.
Inside main.js
import Vue from "vue";
import VueRouter from "vue-router";
import App from "./App.vue";
import HomePage from "./HomePage.vue";
import AboutPage from "./AboutPage.vue";
import AnotherPage from "./AnotherPage.vue";
// This is your router configuration
Vue.use(VueRouter);
const router = new VueRouter({
[
{ path: "/", component: HomePage },
{ path: "/about", component: AboutPage },
{ path: "/another", component: AnotherPage },
],
mode: "history",
});
// Initialize Vue App
new Vue({
router,
render: h => h(App),
}).$mount("#app");
Then, we need to create App.vue and put <router-view /> inside of it.
Inside App.vue source file
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
// Keep this empty. Except if you
// need to add sidebar or any else.
}
</script>
Now you're ready to create those three pages
Every pages looks like this:
<style scoped>
// Your CSS here
</style>
<template>
<div>
<!-- Your HTML here -->
</div>
</template>
<script>
export default {
data() {
return {
// Your reactive data here
}
},
mounted() {
// Your script here
},
methods: {
// Your functions here
},
}
</script>
That's all I can explain, hope it helps. If I missed something, please don't hesitate to tell me. Thank you!

Font Awesome doesn't work inside of a Stencil JS component

Stencil version: #stencil/core#1.3.0
I want to use Font Awesome inside my Stencil component.
I followed these steps from https://fontawesome.com/how-to-use/on-the-web/setup/using-package-managers
Create a "Stencil component starter" project
Install Font Awesome: npm install --save-dev #fortawesome/fontawesome-free
Reference Font Awesome inside src/index.html:
<script src="../node_modules/#fortawesome/fontawesome-free/css/all.css"></script>
<script src="../node_modules/#fortawesome/fontawesome-free/js/all.js"></script>
Add the icon inside my component:
render() {
return (
<button>
<i class="fas fa-camera"></i>
</button>
);
}
I'm not able to include font awesome inside my stencil component. I'm stuck here: <i class="fas fa-camera"></i>
Basically this problem is not only related to font-awesome and stenciljs its a general "custom-font" with "web-components" problem.
Here is a link to the thread with working solution. I Tried it out by myself works perfectly.
https://stackoverflow.com/a/57623658/8196542
I made a demo repo for using icons in stenciljs here:
https://github.com/drygnet/stenciljs-icons-example
Example components with different approaches for:
FontAwesome, Office UI Fabric and Material Icons
I found an elegant way to integrate font awesome with stencilJS.
Install the FontAwesome npm
npm install --save-dev #fortawesome/fontawesome-free;
Then inside your component in your component css file add the #import statement
#import "~#fortawesome/fontawesome-free/css/all.css";
Documentation:
CSS #import
Sample code:
import { Component, Prop, h } from '#stencil/core';
#Component({
tag: 'my-tooltip',
styleUrl: 'my-tooltip.css', // add the #import statement inside the CSS file
shadow: true,
})
export class MyTooltip {
#Prop() text: string;
render() {
return (<div>
<slot />
--
<i class="fa-solid fa-circle-question"></i>
--
</div>);
}
}
------ [my-tooltip.css file] -----
#import "~#fortawesome/fontawesome-free/css/all.css";

Problems with the fullpage.js slider

I have been stuck on this problem for hours and I cant seem to find my answer anywhere on the net... I am working on a vue-cli app, and I am using fullpage.js component with vue.js. I am attempting to create a landscape slider on one section. So far, I followed the instructions on this documentation:
https://github.com/alvarotrigo/fullPage.js#creating-links-to-sections-or-slides
but the slider does not work.
the following is my main.js:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import BootstrapVue from 'bootstrap-vue'
import Carousel3d from 'vue-carousel-3d'
import VueFullPage from 'vue-fullpage.js'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import './assets/css/style.css';
Vue.config.productionTip = false
Vue.use(BootstrapVue);
Vue.use(Carousel3d);
Vue.use(VueFullPage);
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>',
router,
store
})
the following is the vue component I am trying to use this feature on:
<template>
<full-page ref="fullpage" id="fullpage" class="Gallery">
<section class="section">
<div class="slide">
slide 1
</div>
<div class="slide">
slide 2
</div>
</section>
</full-page>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
data(){
return {
half_of_images:Math.ceil(this.$store.state.images.length / 2),
images: this.$store.state.images,
options:{
licenseKey:null,
slidesNavigation: true
}
}
}
}
</script>
<style>
.col , .col > img{
padding:0px !important;
}
.col > img {
border: .5px solid white;
position:relative;
}
</style>
From the docs, https://github.com/alvarotrigo/vue-fullpage.js#bundler-webpack-rollup
Also, you'll have to add the fullPage CSS file (fullpage.min.css). Is up to you how you add it. You can add it on the HTML page or bundle it with other CSS files, or import it with Javascript.
You can do this with an import statement in your vue component
import "fullpage.js/dist/fullpage.css";

How to use css-modules and bootstrap at same time?

I'm using electron-react-boilerplate for my project and this boilerplate is using css-modules for styling purpose.
I'm having trouble with using bootstrap and custom style at the same place.
Suppose i've a code snippet like,
<div className="container">
<div className="row custom-css">
// other codes...
</div>
in that case 'row' is one bootstrap className and 'custom-css' is my own style className.
please help me to find some solution for these problem so that i can use css-modules and bootstrap together...
You need to import your CSS module styles from a module file specific to this component, then interpolate them into the classname via the returned object...
MyComponent.css
.myCustomClassName {
color: #fff;
}
MyComponent.js
import styles from './MyComponent.css';
<div className={`row ${styles.myCustomClassName}`} />
When output as HTML this would become something like...
<div class="row MyComponent__myCustomClassName___n1cC4ge}` />
So as long as you are loading the bootstrap CSS somewhere that should pick up on both
thanks guys i find it working by adding {} around that
<div className={`row ${styles.myCustomClassName}`} />
I was kinda stuck with this (as to how to load Bootstrap).
I created this rough edit in my webpack config file.
{
test: /(\.bootstrap\.css$|bootstrap-theme.css|bootstrap.css)/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
minimize: true || {/* CSSNano Options */}
}
},
],
},
{
test: /^((?!\.bootstrap|bootstrap-theme).)*\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
}
]
},
The rest are covered perfectly by alechill