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

After updating from angular4 to angular 5, the npm test is failing with the error message "TypeScript compilation cannot find test.ts file"

After upgrading my app from Angular4 to Angular 5 using the steps provided on https://update.angular.io/, I encountered an issue. While I can successfully run ng-serve and ng build without any problems, the npm test command for ng test is failing with the ...

The npm start command is unable to recognize the tsx file

I recently attempted to create a React app and incorporate TypeScript into it. However, upon running the app, I noticed that npm start was not recognizing the TypeScript file and failing to automatically generate tsconfig.json. Here is the sequence of ste ...

TS - deduce the specific type of a key value without receiving a union type

Welcome to the coding playground: Click here to start coding Let's talk about a scenario where a function is expected to return some value based on an input argument. The challenge arises when there are keys with the same name but different types re ...

What steps can I take to achieve complete code coverage in my unit testing for this component and also ensure that the 'else' part is being

I am struggling to figure out how to access the remaining line of code in order to achieve full code coverage. Simply checking if the function has been called (toHaveBeenCalled()) will suffice. Here is a snippet from my TypeScript file and Spec file, alon ...

DotLottie file loading issues

While using dotlottie/react-player, webpack 4, and react 16, I encountered module parse failed errors during compilation. "@dotlottie/react-player": "^1.6.5" "webpack": "^4.44.2", "react": "16.14.0&qu ...

In React, the edit mode fails to display class attributes

When I attempted to combine both the creation and editing functionalities in one form, I encountered a problem. Specifically, I was able to retrieve the correct ID value when editing an element, but I struggled to access the attribute values. import { yup ...

Ways to resolve: The JSX component does not contain any construction or call signatures

I've been grappling with a persistent issue regarding the creation of custom elements dynamically in React TypeScript. If you're curious, you can check out the question here. const generalButtons: MenuButton[] = [ { text: "New Cl ...

Troubleshooting TypeScript: Issues with Object.assign and inheritance

After successfully using the code within an Angular project, I decided to switch to React only to find that the code is now producing unexpected results. class A { constructor(...parts: Partial<A>[]) { Object.assign(this, ...parts); } } cla ...

Can an Angular 6 application be integrated into a Umbraco project?

I am currently working on an Umbraco Project that serves as an informative website. Recently, there has been a request to incorporate e-services (interactive HTML forms) into the site, which require either Angular 6 or Ajax. In a previous project, I utili ...

Tips for updating the font size of your MUI Accordion title

I was attempting to adjust the font size of the MUI-5 Accordion title. It seems like I need to override it, but I am unsure about how to do so with CSS in MUI-5. Instead of 'SX', it uses 'htmlSx'. When I tried using it, it did not produ ...

Issue with Angular 4 Routing: Links are opening in new window instead of within router-outlet

I am currently facing an issue while trying to display the SuburbDataComponent HTML on the DASHBOARD-SIDE-PANEL-COMPONENT.HTML. When I click on Dashboard, it opens a new window but only displays the SuburbDataComponent.html without showing the side panel ...

Is there a way to verify if the object's ID within an array matches?

I am looking to compare the ID of an object with all IDs of the objects in an array. There is a button that allows me to add a dish to the orders array. If the dish does not already exist in the array, it gets added. However, if the dish already exists, I ...

Is it recommended to run JavaScript functions obtained from REST APIs?

Our single page application is built on Angular 4 and we are able to change input fields based on customer requirements. All the business rules for adjusting these fields are coded in JavaScript, which runs on the Java Platform and delivers the output thro ...

The KeyConditionExpression is invalid due to the use of multiple attribute names within a single condition

I am trying to query a DynamoDB table using GraphQL TableName: "JobInfo", IndexName: "tableauGSI", KeyConditionExpression: "tableauGSI_Tableau = tableau AND #D BETWEEN :startDate AND :endDate", ExpressionAttributeNames: { "#D": "date" }, ...

Unlock the Power of Angular with Custom Decorators: Accessing ElementRef Made Easy

I am currently working on implementing a decorator for Host CSS Variable Binding in Angular5. However, I am facing difficulties in properly implementing it with the given code. Is there a way to define ElementRef from within the decorator itself? export f ...

JavaScript Definition File for TypeScript

Within my repertoire is a Js File, comprised of a leaflet plugin, Js: L.BingLayer = L.TileLayer.extend({ options: { subdomains: [0, 1, 2, 3], type: 'Aerial', attribution: 'Bing', culture: '' }, initialize ...

How can I import multiple variables in TypeScript?

I have a React application built with TypeScript, and my project directory is structured as follows: App.tsx /pages Page1.tsx The contents of Page1.tsx are shown below: Page1.tsx class PageParams { constructor() { } } class Page1 { co ...

Stopping Angular 2 Service Request

I'm working on a menu with category tabs and the following function that gets triggered upon click. this._customerservice.GetCustomersByFilter(this.FilterOptions) //Get Customers by Page Number and CategoryId .subscribe(res => { ...

How to dynamically disable a checkbox in Angular reactive forms depending on the value of another checkbox

I am attempting to deactivate the checkbox based on the value of other checkboxes. Only one of them can be activated at a time. When trying to activate one of the checkboxes, I encounter an issue where the subscribed value is being repeated multiple times ...

Is there a feature similar to notifyWhenNoOutstandingRequests in Angular 2?

In my experience, test frameworks like Arquillian have utilized this method to determine when the DOM is ready for inspection with Angular 1. I am curious if there is a similar approach for accomplishing this in Angular 2? ...