Create a unique custom design for your Mapbox GL control

When developing the website, we utilized Angular 8, Typescript, and SCSS. We are using mgl-map to display a map, and I wanted to create a custom control for it with unique styles.

I added the custom control to the map using:

  const centerOnCoordinatesControl = new CenterOnCoordinatesControl();
  this.mapBoxMap.addControl(centerOnCoordinatesControl, 'bottom-left');

While this implementation works fine, my custom styles do not get applied to the control.

https://i.sstatic.net/a1Yj4.png

To verify if the CSS styles are being loaded correctly, I placed the same element outside the map control:

https://i.sstatic.net/XJZdL.png

location.component.html:

<mgl-map class="mapbox-map"
         [style]="defaultStyle"
         [zoom]="zoom"
         [center]="grazCoord"
         (click)="onMapClick($event)"
         (load)="mapLoaded($event)">
         <mgl-control mglScale unit="metric" position="bottom-right"></mgl-control>
         <mgl-control *ngIf="this.mapBoxMap" mglFullscreen position="top-right"></mgl-control>
         <mgl-control *ngIf="this.mapBoxMap" mglGeolocate position="top-right"></mgl-control>
</mgl-map>

location.component.ts:

CustomControl:

class CenterOnCoordinatesControl {
  private map: any;
  private container: HTMLDivElement; 

  onAdd(map) {
    this.map = map;
    this.container = document.createElement('div');
    const control = document.createElement('i');

    control.className = 'custom-center-control mapboxgl-ctrl fas fa-map-marker-alt pointer'; 

    this.container.appendChild(control);
    return this.container;
  } 
}

location.component.scss:

.custom-center-control {
    font-size: x-large;
    color: green;

    &:hover {
        color: $cities-orange-light;
    }
}

I'm struggling to understand why the style is applied to the element outside the map but not to the CustomControl inside the map.

Is there a way to make it work?

Please let me know if you require more details.

Answer №1

Give the encapsulation technique a shot by adding the following line to your decorator:

For instance:

@Component({
  selector: 'my-custom-input',
  templateUrl: 'src/my-custom-input.component.html',
  styleUrls:['src/my-custom-input.component.css'],
  encapsulation: ViewEncapsulation.None
})

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

The error message indicates a compatibility issue between parameters 'r' and 'a'

Attempting to reproduce the code found in this blog post, but encountering some perplexing errors. import { option, identity, apply } from 'fp-ts'; import type { Kind, URIS } from 'fp-ts/HKT'; import { pipe } from 'fp-ts/lib/functi ...

Incorporate Ng-Survey multiple times within one component

Incorporating the ng-surveys template into my Angular application via has been successful. However, I encountered an issue where when using the template selector *ngFor to display multiple surveys on the same page, the browser treats all the surveys as id ...

Angular 11 error: Datatable type is not defined

We have a services.ts file that requires 2 arguments. UPDATE: It appears that I was looking at the error incorrectly. This method is causing the console error: private checkLink(row, options) { if (options.type === 'link' || options.type ...

Personalized color scheme for calendar td

I am facing an issue with my event calendar where I want to change the background color of td. I came across a helpful solution in this question, but it did not fully solve my problem. When I implemented the following jquery: $('[class*="fc"]'). ...

Adjusting the size of the entire webpage using Bootstrap 5

My webpage is built using Bootstrap 5 and overall, I am pleased with how it looks. However, I'm finding that all the body elements are too large. View Elements too large The elements at the bottom of the page are getting cut off and I want the entir ...

Clickable link remains functional after being hidden

Even though I have hidden the Games-div using the .hide() function in jQuery, my links are still clickable. Is there a way to make them unclickable after being hidden? Here is the link to my code on jsFiddle: http://jsfiddle.net/hypertje/Frv8G/ Note: The ...

Troubleshooting an Issue with CSS Sliding Doors

I'm having trouble getting the right image to show on my website. The left side of the image is fine, but I can't figure out how to make the right one appear. As a HTML/CSS beginner, I'm still learning the basics. For more information, vis ...

Align two grid columns in the centre of the footer

I have a question regarding the alignment of two grid columns in my footer. I am using Bootstrap 4 and would like to center them directly in the middle. Currently, both columns are centered individually but there is a large space between them. I have tried ...

Using query parameters in Angular to interact with APIs

One scenario involves a child component passing form field data to a parent component after a button press. The challenge arises when needing to pass these fields as query parameters to an API endpoint API GET /valuation/, where approximately 20 optional p ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

A guide on transitioning from using require imports to implementing ES6 imports with the concept of currying

Currently in the process of migrating a Node/Express server to TypeScript. I have been using currying to minimize import statements, but now want to switch to ES6 import syntax. How can I translate these imports to ES6? const app = require("express")(); ...

The call stack size has reached its maximum limit;

Encountering an issue with the use of componentDidMount(). This method is intended to display a Tooltip by utilizing the function _getContentTooltip(). However, the problem arises as it triggers the error message common.js:444 RangeError: Maximum call st ...

"The issue persists in Angular 5 - I am using ngOnInit to fetch my object from firestore, but surprisingly the view does not reflect any

After working with Angular 5 and Firestore, I encountered an interesting issue. When retrieving my document and checking it using console.log(this.assets) in ngOnInit(), everything seems fine. However, a strange thing happens during a page reload: if I qui ...

Unable to extend baseclass in component class in Angular 4

I am a beginner in Angular and I am attempting to extend a base class with a component class. Here is what the base class looks like: export class QuestionBase<T>{ value?: T; key?: string; label?: string; required?: boolean; order?: number ...

Is it feasible to decrease the lateral margins of Bootstrap's container without the need for custom CSS? If so, what is the method?

My web page includes several screenshots to illustrate the issue I am facing. One of my challenges is the scroll bar below the table, which I find displeasing. In an attempt to resolve this, I decided to adjust the lateral margins slightly. Here's a s ...

When displaying content within an iframe, toggle the visibility of div elements

I'm attempting to toggle the visibility of certain page elements based on whether the page is loaded in an iframe. <script>this.top.location !== this.location && (this.top.location = this.location);</script> The above code succes ...

Enclose an <img> tag within a <span> element and use jQuery to assign a class to the <span>

I am trying to wrap a specific <img> tag with a <span> element and assign a class to the <span>. Below is the relevant code snippet: <img id="wrapped-image" alt="" src=""> Despite my efforts, the following code does not seem to w ...

Guide to utilizing vue-twemoji-picker in TypeScript programming?

Has anyone encountered this issue when trying to use vue-twemoji-picker in a Vue + TypeScript project? I keep receiving the following error message. How can I fix this? 7:31 Could not find a declaration file for module '@kevinfaguiar/vue-twemoji-picke ...

Encountering a clash in Npm dependencies

I have been involved in a Vue project where I utilized Vue Cli and integrated the Typescript plugin. However, I encountered multiple vulnerabilities that need to be addressed. When I executed npm audit fix, it failed to resolve the dependency conflict: npm ...

Is it possible for a typed function to access object properties recursively?

Is there an efficient method in TypeScript to type a function that can recursively access object properties? The provided example delves two levels deep: function fetchNestedProperty<T, K extends keyof T>(obj: T, prop1: K, prop2: keyof T[K]) { r ...