Skip to content

Commit f0d03d8

Browse files
authored
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.
1 parent 43d3485 commit f0d03d8

10 files changed

Lines changed: 33 additions & 15 deletions

File tree

README.md

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

127-
**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/
128+
**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/
128129

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

projects/demo_app/src/app/pages/dashboard/dashboard.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
<section class="dashboard-grid" aria-label="Analytics dashboard">
1111
<article class="dashboard-card dashboard-wide">
1212
<header><div><p>Revenue</p><h2>Monthly performance</h2></div><span class="status-pill">USD</span></header>
13-
<div class="dashboard-plot"><plotly-plot [data]="revenueData" [layout]="revenueLayout" [config]="config" [style]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
13+
<div class="dashboard-plot"><plotly-plot [data]="revenueData" [layout]="revenueLayout" [config]="config" [innerStyle]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
1414
</article>
1515
<article class="dashboard-card">
1616
<header><div><p>Customers</p><h2>Plan mix</h2></div></header>
17-
<div class="dashboard-plot"><plotly-plot [data]="segmentData" [layout]="segmentLayout" [config]="config" [style]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
17+
<div class="dashboard-plot"><plotly-plot [data]="segmentData" [layout]="segmentLayout" [config]="config" [innerStyle]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
1818
</article>
1919
<article class="dashboard-card">
2020
<header><div><p>Acquisition</p><h2>Channel comparison</h2></div></header>
21-
<div class="dashboard-plot"><plotly-plot [data]="channelData" [layout]="channelLayout" [config]="config" [style]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
21+
<div class="dashboard-plot"><plotly-plot [data]="channelData" [layout]="channelLayout" [config]="config" [innerStyle]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
2222
</article>
2323
<article class="dashboard-card dashboard-wide">
2424
<header><div><p>Engagement</p><h2>Sessions by weekday</h2></div></header>
25-
<div class="dashboard-plot"><plotly-plot [data]="heatmapData" [layout]="heatmapLayout" [config]="config" [style]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
25+
<div class="dashboard-plot"><plotly-plot [data]="heatmapData" [layout]="heatmapLayout" [config]="config" [innerStyle]="plotStyle" [useResizeHandler]="true"></plotly-plot></div>
2626
</article>
2727
</section>
2828

projects/demo_app/src/app/pages/events/events.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[data]="data"
1111
[layout]="layout"
1212
[config]="config"
13-
[style]="plotStyle"
13+
[innerStyle]="plotStyle"
1414
[useResizeHandler]="true"
1515
(plotlyClick)="capture('plotlyClick', $event)"
1616
(hover)="capture('hover', $event)"

projects/demo_app/src/app/pages/getting-started/getting-started.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[data]="data"
1111
[layout]="layout"
1212
[config]="config"
13-
[style]="plotStyle"
13+
[innerStyle]="plotStyle"
1414
[useResizeHandler]="true">
1515
</plotly-plot>
1616
</div>

projects/demo_app/src/app/pages/getting-started/getting-started.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class GettingStartedComponent {
5555
[data]="data"
5656
[layout]="layout"
5757
[config]="config"
58-
[style]="plotStyle"
58+
[innerStyle]="plotStyle"
5959
[useResizeHandler]="true">
6060
</plotly-plot>`;
6161

projects/demo_app/src/app/pages/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h1>Interactive charts.<br><span>Angular-native.</span></h1>
3535
[data]="data"
3636
[layout]="layout"
3737
[config]="config"
38-
[style]="plotStyle"
38+
[innerStyle]="plotStyle"
3939
[useResizeHandler]="true">
4040
</plotly-plot>
4141
</div>

projects/demo_app/src/app/pages/lifecycle/lifecycle.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[data]="data"
2222
[layout]="layout"
2323
[config]="config"
24-
[style]="plotStyle"
24+
[innerStyle]="plotStyle"
2525
[useResizeHandler]="true"
2626
[debug]="debugEnabled"
2727
(initialized)="record('initialized')"

projects/demo_app/src/app/pages/reactive-updates/reactive-updates.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
[data]="data"
2121
[layout]="layout"
2222
[config]="config"
23-
[style]="plotStyle"
23+
[innerStyle]="plotStyle"
2424
[useResizeHandler]="true"
2525
[revision]="revision"
2626
[updateOnlyWithRevision]="true"

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
@@ -30,7 +30,7 @@ import { Plotly } from './plotly.interface';
3030
selector: 'plotly-plot',
3131
standalone: true,
3232
imports: [CommonModule],
33-
template: `<div #plot [attr.id]="divId()" [ngClass]="getClassName()" [ngStyle]="style()">
33+
template: `<div #plot [attr.id]="divId()" [ngClass]="getClassName()" [ngStyle]="innerStyle() ?? style()">
3434
<ng-content></ng-content>
3535
</div>`,
3636
changeDetection: ChangeDetectionStrategy.Eager,
@@ -51,6 +51,10 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
5151
layout = input<Partial<Plotly.Layout>>();
5252
config = input<Partial<Plotly.Config>>();
5353
frames = input<Partial<Plotly.Config>[]>();
54+
innerStyle = input<{ [key: string]: string }>();
55+
/**
56+
* @deprecated Use `innerStyle` to avoid conflicting with Angular's global style binding.
57+
*/
5458
style = input<{ [key: string]: string }>();
5559

5660
divId = input<string>();

0 commit comments

Comments
 (0)