What is the process for personalizing the appearance in cdk drag and drop mode?

I have created a small list of characters that are draggable using Cdk Drag Drop. Everything is working well so far! Now, I want to customize the style of the draggable items. I came across .cdk-drag-preview class for styling, which also includes box-shadow. I attempted to modify the mat-checkboxes within .cdk-drag-preview but it didn't work as expected. Do you have any suggestions on how to achieve this?

Check out my Stackblitz demo: https://stackblitz.com/edit/angular-cdk-drag-drop-vsge6t?file=app%2Fcdk-drag-drop-connected-sorting-example.css

Here is my code:

// HTML

<form [formGroup]="reportCompositionForm">
  <div class="drag-container">
    <div cdkDropListGroup>
      <div
        cdkDropList
        [cdkDropListData]="displayCharactersArray"
        class="item-list"
        (cdkDropListDropped)="drop($event)"
      >
        <div
          class="item-box"
          *ngFor="let element of displayCharactersArray; let i = index"
          cdkDrag
        >
          <mat-checkbox (change)="onChange($event)" [value]="element">
            <span (click)="deactivateCheckBoxClickEvent($event)">
              {{ element.viewValue }}
            </span>
          </mat-checkbox>
        </div>
      </div>
    </div>
  </div>
</form>
// CSS
.drag-container {
  display: inline-block;
  vertical-align: top;
  max-width: 100%;
  width: 400px;
}

.item-list {
  display: block;
}

.item-box {
  display: flex;
  font-size: 14px;
  color: #ffffff;
  background-color: #9594947a;
  border: 1px solid #9594947a;
  border-radius: 4px;
  margin-bottom: 5px;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  cursor: move;
}

.cdk-drag-preview {
  box-sizing: border-box;
  border-radius: 4px;
  box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
    0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12);
}

.cdk-drag-placeholder {
  opacity: 0;
}

.cdk-drag-animating {
  transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}

.item-list.cdk-drop-list-dragging .item-box:not(.cdk-drag-placeholder) {
  transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}

:host ::ng-deep .item-box mat-checkbox label.mat-checkbox-layout {
  margin-left: 5px;
  margin-bottom: 1px;
}

:host
  ::ng-deep
  .item-box
  mat-checkbox
  label.mat-checkbox-layout
  span.mat-checkbox-label
  span {
  cursor: move;
}

:host
  ::ng-deep
  .item-box
  mat-checkbox
  label.mat-checkbox-layout
  span.mat-checkbox-inner-container
  .mat-checkbox-frame {
  border-color: #e1dfdf;
}

:host
  ::ng-deep
  .item-box
  mat-checkbox
  label.mat-checkbox-layout
  span.mat-checkbox-inner-container
  .mat-checkbox-ripple
  .mat-ripple-element {
  background-color: transparent;
}

:host
  ::ng-deep
  .item-box
  .mat-checkbox-checked.mat-accent
  .mat-checkbox-background,
.mat-checkbox-indeterminate.mat-accent .mat-checkbox-background {
  background-color: transparent;
}

Answer №1

To apply draggable mode styles, simply utilize ng-deep without host. Alternatively, for static styles that should only be applied when items are not being dragged, use :host ::ng-deep

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When you use map to transform the value, Observable will consistently return as undefined

Can anyone help me figure out why, when I transform the observable, it returns undefined in the console log even though there are assigned values? HTTP Request getLibraryCardPeople(bookName: String): Observable<people[]> { return this.http.get<Li ...

Configuring rows in React datagrid

I am working on a component where I receive data from the backend and attempt to populate a DataGrid with it. Below is the code for this component: export const CompaniesHouseContainer: React.FC<Props> = () => { const classes = useStyl ...

A guide on iterating through a JSON object fetched using Http in Angular 2/Typescript

I am attempting to extract specific data from my JSON file using http. The structure of the JSON is as follows: [{"name":"Name1","perc":33},{"name":"Name2","perc":22},{"name":"Name3","perc":41}] To loop through this retrieved object, I use the following ...

Enhance your Next.js routing by appending to a slug/url using the <Link> component

In my Next.js project, I have organized my files in a folder-based structure like this: /dashboard/[appid]/users/[userid]/user_info.tsx When using href={"user_info"} with the built-in Next.js component on a user page, I expect the URL to dynamic ...

Is it possible to use jQuery to set a value for a form control within an Angular component?

I'm currently working on an Angular 5 UI project. In one of my component templates, I have a text area where I'm attempting to set a value from the component.ts file using jQuery. However, for some reason, it's not working. Any suggestions o ...

The EventEmitter in Angular 8 is prohibiting the emission of an Object

Is there a way I can emit an Object instead of primitive data types? I have an object const amount = { currenty: 'USD', total: '10.25' }; that I need to emit to the parent component. export class MyChildComponent implements OnInit { ...

Setting up a Form submit button in Angular/TypeScript that waits for a service call to finish before submission

I have been working on setting up a form submission process where a field within the form is connected to its own service in the application. My goal is to have the submit button trigger the service call for that specific field, wait for it to complete suc ...

What does it signify when it is stated that "it is not a descendant of the indexer"?

Currently, I am diving into Typescript with the help of this informative guide on indexer types. There is a specific piece of code that has me puzzled: interface NumberDictionary { [index: string]: number; length: number; // okay, length shoul ...

Forbidden access to Angular 4 web application on S3 following redirection

Recently, I developed a basic angular 4 application that utilizes firebase for authentication. Due to issues with loginWithPopup on my mobile device, I switched to using loginWithRedirect. However, the problem arises when the redirect takes me away from t ...

Typescript fails to identify the parameter type of callbacks

I am facing a challenge with the function below and its callback type: type Callbacks = { onSuccess: (a: string) => void; }; function myFunction(event: string, ...args: [...any, Callbacks]) { } The function works correctly except for one issue - ...

Issue with Angular / SCSS where animation is not being executed on the specified element

I have created a post component with a comment section. However, when I click on the comments, I want them to display smoothly with a nice animation effect. Even though I have added the necessary code for animations, it doesn't seem to work as expecte ...

Steps for reinstalling TypeScript

I had installed TypeScript through npm a while back Initially, it was working fine but turned out to be outdated Trying to upgrade it with npm ended up breaking everything. Is there any way to do a fresh installation? Here are the steps I took: Philip Sm ...

Why is the quantity of my item being increased? My method is adding it when it shouldn't be

I have a project in Angular that involves an online store. However, every time I click the button "agregar a carrito" (add to cart in Spanish), my item's quantity seems to increase inexplicably. ts. removeItem(item: iProduct) { if (item.quantity ...

I need help accessing the information for every user within my nested Firestore database using Ionic4 and Angular. How can I retrieve this data?

I am currently working on extracting user data from a nested Firestore database using Angular. The database has a structure where users (id) have posts(id) with fields like {description: this is an image, image: imageURL}. In my Ionic app, I am utilizing ...

The Angular Progressive Web App functions properly in ng serve mode, but encounters issues when running with http-server

I'm developing a Progressive Web App (PWA) using Angular. Everything was functioning smoothly until out of nowhere, I started encountering a 404 Error whenever I tried to navigate to a new component while serving in dist/project with http-server. Surp ...

The browser is failing to send cookies to the backend and is also not properly setting them initially

Greetings everyone, I am currently in the process of securing my spring boot application with HTTP only cookies, transitioning from local storage. However, I have encountered some challenges along the way that even chatGPT couldn't resolve :) Let&apo ...

Ways to Update the color of every element using a specified directive once a new theme is selected by the user

In the process of developing an online document builder using angular 4, I have created directives such as my-header and my-subheader to control the CSS styling properties of the angular components, like font size and boldness. My current challenge is cha ...

Guide on how to retrieve the information stored in an object

I am experiencing an issue with my function that retrieves data from Firebase. I am able to read the objects, but I cannot access the properties within them. Whenever I try to parse the content, an error occurs. Here is the function in question: this ...

What is the method to obtain the complete URL in Angular?

I'm exploring ways to utilize Angular Universal in my app and I am seeking a method to retrieve the complete path of the current url within an Angular component. Initially, I considered tapping into the window object which would involve injecting it o ...

Observable<void> fails to trigger the subscriber

I am currently facing a challenge with implementing a unit test for an Observable in order to signal the completion of a process. While there is no asynchronous code in the logout function yet, I plan to include it once the full logic is implemented. The m ...