I have been struggling trying to make TailwindCSS work with Angular. I followed some tutorials and looked in the documentation of Tailwind. For some reason in certain places in the Angular 9 application it's working and in lazy loaded modules it's not (so it seems)!
So first thing I did was following and implementing these tutorials:
https://dev.to/seankerwin/angular-8-tailwind-css-guide-3m45
https://dev.to/beavearony/building-tailwind-css-in-an-angular-cli-project-e04
I know for sure I have these installed correctly because of the following:
My sidebar(app-sidebar) is having the correct css and styling! But the page I'm on loaded through the is not.
I will provide you with my app-routing.module, and the default layout and the dashboard component. On this Dashboard the tailwindCSS is not loading. (Funny thing: if I add an H1 element with no class, no nothing I see this element on the dashboard page!) The other elements which contain some kind of tailwindCSS class don't. Also if I drag-and-drop through the content of my dashboard component to outside this dashboard component I see my elements, although without any styling. (I do this with Chrome DevTools)
app-routing.module.ts
{
path: 'login',
loadChildren: () => import('./modules/login/login.module').then(m => m.LoginModule)
},
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full',
},
{
path: '',
component: DefaultLayoutComponent,
children:[
{
path: 'dashboard',
loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule)
}
]
},
{ path: '**', redirectTo: '' }
];
default-layout.component.html
<div class="h-screen flex overflow-hidden bg-gray-100">
<app-sidebar></app-sidebar>
<div id="content">
<router-outlet></router-outlet>
</div>
</div>
dashboard.component.html
<h1>Dashboard</h1>
<div class="flex flex-col w-0 flex-1 overflow-hidden">
<div class="md:hidden pl-1 pt-1 sm:pl-3 sm:pt-3">
<button class="-ml-0.5 -mt-0.5 h-12 w-12 inline-flex items-center justify-center rounded-md text-gray-500 hover:text-gray-900 focus:outline-none focus:bg-gray-200 transition ease-in-out duration-150" aria-label="Open sidebar">
<svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
<main class="flex-1 relative z-0 overflow-y-auto pt-2 pb-6 focus:outline-none md:py-6" tabindex="0">
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<h1 class="text-2xl font-semibold text-gray-900">Dashboard</h1>
</div>
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
<!-- Replace with your content -->
<div class="py-4">
<div class="border-4 border-dashed border-gray-200 rounded-lg h-96"></div>
</div>
<!-- /End replace -->
</div>
</main>
</div>
The following I have tried but no succes:
ViewEncapsulation (How to get Angular's <router-outlet> to work with CSS Grid)
Added on each page a link to the CDN with tailwindCSS
Does it maby has any thing to do with the lazy loaded module?
Or maby that there is a double router-outlet?
Because this is how my app.component.html looks
<router-outlet></router-outlet>
So I have figured it out. There were actually two things left I had to do.
First was to set the tailwindcss configuration (tailwind.config.js) and attach it to tailwindcss with the help of my webpack.config.js
module.exports = {
module : {
rules: [
{
test : /\.scss$/,
loader : 'postcss-loader',
options: {
ident : 'postcss',
syntax: 'postcss-scss',
plugins: () => [
require('postcss-import'),
require('tailwindcss')('./tailwind.config.js'),
require('autoprefixer')
]
}
}
]
}
};
Second thing I had to do was putting the overall class of the component to the host in my Angular Component.
dashboard.component.ts
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
host: {
class: "flex flex-col w-0 flex-1 overflow-hidden"
}
})
export class DashboardComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
So looking back at my question and looking at the dashboard.component.html file, the highest div is removed and the class of that div is placed in host! Like above.
I have followed the first link in my question!
Related
I am trying to apply an onhover effect for a viewport larger than 768px. Below 768px it should look like the elements are "always onhover".
Specifically, on desktop or larger devices in general, there should be a background in a box at onhover. This background should always be shown on mobile, i.e. <768px in my case.
What am I doing wrong?
Here are the snippets.
JSX-Snippet:
import { features } from "../constants"; import styles from "../style";
const FeatureCard = ({ icon, title, content, index }) => (
<div className=
{`
flex flex-row p-6 rounded-[20px]
ss:max-sm:feature-card-small
sm:feature-card
${index !== features.length - 1 ? "mb-6" : "mb-0"}
`}
>
<div className={`
w-[64px]
h-[64px]
rounded-full
${styles.flexCenter}
bg-dimBlue`
}>
<img
src={icon}
alt="star"
className="
w-[50%]
h-[50%]
object-contain"
/>
</div>
<div className="flex-1 flex flex-col ml-3">
<h4 className="
font-poppins
font-semibold
text-white
text-[18px]
leading-[23.4px]
mb-1">
{title}
</h4>
<p className="
font-poppins
font-normal
text-dimWhite
text-[16px]
leading-[24px]
">
{content}
</p>
);
export default FeatureCard;
CSS-Snippet:
.feature-card:hover { background: var(--black-gradient); box-shadow: var(--card-shadow); }
.feature-card-small { background: var(--black-gradient); box-shadow: var(--card-shadow); }
simply apply feature-card as class without viewport specification works as expected; onhover --> background changes
different viewport-specifications (sm:..., ss:max-sm and sm:) and creation of different CSS classes (so there can be no overlap for sure) did not work
sorry if the code block looks like a mess here on stackoverflow. my first post, still learning.
I am trying to make tooltips for icons in my header, and right now I have made a component to make the icon and tooltip into one div, with tailwind styles in that
JSX Component:
const HeaderIcon = ({ icon, redirect = window.location.pathname.replace('/', ""), text = ""}:IconProps) => (
<div className="group relative flex items-center justify-center h-12 w-12 mt-2 mb-2 mx-5 cursor-pointer">
<button onClick={() => goto(redirect)} className="items-center inline-flex">{icon}</button>
<span className="group-hover:visible absolute rounded-md shadow-md text-white bg-gray-900 text-xs font-bold transition-all duration-100 p-2 text-center min-w-max invisible">
{text}
</span>
</div>
)
Here goto is a redirect function, icon is a JSX element from react-feather (icon library), redirect is a string telling the button where to redirect to, and text is the tooltip text. What tailwind classes should I use to position the tooltips under the icons, centered.
If there is any other things you may want then just ask and I will edit the code in.
you need to create a new variant group-hover for the plugin visibility in the Tailwind configuration:
// tailwind.config.js
module.exports = {
// ...
variants: {
extend: {
visibility: ['group-hover'],
}
},
}
I am using Tailwind CSS with Next.js, and want to show progress bar in below url.
https://www.tailwind-kit.com/components/progress
The code is below:
<div>
<div class="bg-white rounded-lg w-72 shadow block p-4 m-auto">
<div class="w-full h-2 bg-gray-400 rounded-full mt-3">
<div class="w-3/4 h-full text-center text-xs text-white bg-green-500 rounded-full">
</div>
</div>
</div>
</div>
However, If I check browser, the bar is not displayed and the height of the div element is 0.
I checked in Chrome and Safari, and the result was the same in both.
I want to know how to show the empty div element in browser.
--Update:
I have solved the above problem by removing the height customize from tailwind.config.js.
module.exports = {
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
darkMode: false, // or 'media' or 'class'
theme: {
// removed this
height: {
main: '681px'
},
},
variants: {
extend: {},
},
plugins: [],
}
However, I don't know why the height customize caused this problem, does anyone know about it?
you could use grow Property:
<div class="grow w-3/4 h-full ....">
so I've made this simple popup with tailwindcss that will show project description.
This is how it looks:
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import Button from "../../../components/elements/Button";
import ReactMarkdown from "react-markdown";
import { Repo } from "../../../lib/types/Repos";
interface PageProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
project: Repo;
}
export default function ProjectDescription({ setIsOpen, project }: PageProps) {
useEffect(() => {
document.body.style.overflow = "hidden";
}, []);
const close = () => {
document.body.style.overflow = "auto";
setIsOpen(false);
};
return (
<div
onClick={close}
className="fixed inset-0 h-full w-full flex z-20 justify-center items-center p-4 cursor-pointer"
style={{ backgroundColor: "rgba(0,0,0,0.3)" }}
>
<div className="bg-gray-50 shadow-md scrollbar z-[9999999] max-w-xl max-h-[600px] w-full h-auto px-8 py-8 overflow-auto rounded-md">
<div className="flex justify-between items-center">
<p className="text-gray-700">
Project built with:{" "}
<strong className="text-indigo-600">{project.language}</strong>
</p>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-8 w-8 text-gray-900 cursor-pointer"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
onClick={close}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</div>
{project.readme && (
<ReactMarkdown
children={project.readme}
className="prose prose-indigo"
/>
)}
<div className="mt-4">
<Button>Webiste</Button>
<a className="ml-4">Github</a>
</div>
</div>
</div>
);
}
Now the problem is that this container(upper div) has click event, and first div inside him also has same event??? Basically I can click on div inside and close modal. And that should not happen? Could someone please explain what's happening.
If you give onClick event to parent div you call same event by click the child dive. It is because the first thing you click is again will be the parent. Give z-9999 insted of z-[9999999] to the child div. Usually what you want to do when making a modal is set onClick(close) event to button not to entirly div itself. In your case you can set onHide(close) to parent div. With this way you close the modal by clicking outside of the modal.
I created a component named Header with a simple css class:
<template>
<nav
class="flex fixed w-full items-center justify-between px-6 h-16 bg-white text-gray-700 border-b border-gray-200 z-10"
>
<!-- Etc... -->
</nav>
In Home component I registred Header but it is overlapping the home:
<template>
<div class="container">
<Header />
<div class="flex m-5">
<h3>Hello</h3>
</div>
</div>
</template>
<script>
export default {
name: 'Home',
components: {
Header: () => import('#/components/Header.vue')
}
}
</script>
The Hello is behind, even including block class in Home component is not worked. Anyone can helped?
There are may ways you could achieve this, but building on the code you already have, you could:
Add a top-0 class to your header. This will ensure that your header which is now positioned fixed will stick to the top of the viewport.
Add a top padding class equavliant to the height of your header (e.g. pt-16) to your container.
Here's a live demo for your reference.
overlapping component each others because of height of your component and in flex height taking automaticly so remove your height
remove css h-16