Skip to content

Commit d2f1f9e

Browse files
bzh2610andrefarzat
authored andcommitted
feat: add innerStyle input without breaking style compatibility (#289)
Adds innerStyle as the preferred input while retaining style as a deprecated compatibility alias. Updates documentation, demo usage, and tests.\n\nCloses #287. (cherry picked from commit f0d03d8) (cherry picked from commit 6a350f2)
1 parent 4ab261d commit d2f1f9e

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,19 @@ For a full description of Plotly chart types and attributes see the following re
122122
| `(error)` | `Function(err)` | `undefined` | Callback executed when a plotly.js API method rejects |
123123
| `[divId]` | `string` | `undefined` | id assigned to the `<div>` into which the plot is rendered. |
124124
| `[className]` | `string` | `undefined` | applied to the `<div>` into which the plot is rendered |
125-
| `[style]` | `Object` | `{position: 'relative', display: 'inline-block'}` | used to style the `<div>` into which the plot is rendered |
125+
| `[innerStyle]` | `Object` | `{position: 'relative', display: 'inline-block'}` | used to style the `<div>` into which the plot is rendered |
126+
| `[style]` | `Object` | `undefined` | deprecated compatibility alias for `[innerStyle]` |
126127
| `[debug]` | `Boolean` | `false` | Assign the graph div to `window.gd` for debugging |
127128
| `[useResizeHandler]` | `Boolean` | `false` | When true, adds a call to `Plotly.Plot.resize()` as a `window.resize` event handler |
128129

129-
**Note**: To make a plot responsive, i.e. to fill its containing element and resize when the window is resized, use `style` or `className` to set the dimensions of the element (i.e. using `width: 100%; height: 100%` or some similar values) and set `useResizeHandler` to `true` while setting `layout.autosize` to `true` and leaving `layout.height` and `layout.width` undefined. This will implement the behaviour documented here: https://plot.ly/javascript/responsive-fluid-layout/
130+
**Note**: To make a plot responsive, i.e. to fill its containing element and resize when the window is resized, use `innerStyle` or `className` to set the dimensions of the element (i.e. using `width: 100%; height: 100%` or some similar values) and set `useResizeHandler` to `true` while setting `layout.autosize` to `true` and leaving `layout.height` and `layout.width` undefined. This will implement the behaviour documented here: https://plot.ly/javascript/responsive-fluid-layout/
130131

131132
```typescript
132133
@Component({
133134
selector: 'plotly-example',
134135
template: `
135136
<plotly-plot [data]="graph.data" [layout]="graph.layout"
136-
[useResizeHandler]="true" [style]="{position: 'relative', width: '100%', height: '100%'}">
137+
[useResizeHandler]="true" [innerStyle]="{position: 'relative', width: '100%', height: '100%'}">
137138
</plotly-plot>`,
138139
})
139140
export class PlotlyExampleComponent {

projects/plotly/src/lib/plotly.component.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,25 @@ describe('PlotlyComponent', () => {
4141
expect(component.plotEl.nativeElement).toBeDefined();
4242
});
4343

44-
it('should receive the style from the property', () => {
44+
it('should receive the inner style from the property', () => {
45+
componentRef.setInput('innerStyle', { 'background-color': 'red' });
46+
fixture.detectChanges();
47+
expect(component.plotEl.nativeElement.style.backgroundColor).toBe('red');
48+
});
49+
50+
it('should retain style as a deprecated compatibility alias', () => {
4551
componentRef.setInput('style', { 'background-color': 'red' });
4652
fixture.detectChanges();
4753
expect(component.plotEl.nativeElement.style.backgroundColor).toBe('red');
4854
});
4955

56+
it('should prefer innerStyle when both style inputs are provided', () => {
57+
componentRef.setInput('style', { 'background-color': 'red' });
58+
componentRef.setInput('innerStyle', { 'background-color': 'blue' });
59+
fixture.detectChanges();
60+
expect(component.plotEl.nativeElement.style.backgroundColor).toBe('blue');
61+
});
62+
5063
it('should add the id in the #plotEl', () => {
5164
expect(component.plotEl.nativeElement.id).toBe('');
5265
componentRef.setInput('divId', 'some-id');

projects/plotly/src/lib/plotly.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Plotly } from './plotly.interface';
2929
selector: 'plotly-plot',
3030
standalone: true,
3131
imports: [CommonModule],
32-
template: `<div #plot [attr.id]="divId()" [ngClass]="getClassName()" [ngStyle]="style()">
32+
template: `<div #plot [attr.id]="divId()" [ngClass]="getClassName()" [ngStyle]="innerStyle() ?? style()">
3333
<ng-content></ng-content>
3434
</div>`,
3535
providers: [PlotlyService],
@@ -49,6 +49,10 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
4949
layout = input<Partial<Plotly.Layout>>();
5050
config = input<Partial<Plotly.Config>>();
5151
frames = input<Partial<Plotly.Config>[]>();
52+
innerStyle = input<{ [key: string]: string }>();
53+
/**
54+
* @deprecated Use `innerStyle` to avoid conflicting with Angular's global style binding.
55+
*/
5256
style = input<{ [key: string]: string }>();
5357

5458
divId = input<string>();

0 commit comments

Comments
 (0)