Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ember-context #70

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .lint-todo
Original file line number Diff line number Diff line change
Expand Up @@ -1195,3 +1195,7 @@ add|ember-template-lint|no-action|481|34|481|34|bfda11268adacee3d85d949191eaef65
add|ember-template-lint|no-curly-component-invocation|478|14|478|14|288855346142f21a93fc97d3cc3f023dc64d6fc0|1694649600000|||tests/integration/components/polaris-resource-list/filter-control/filter-value-selector-test.js
add|ember-template-lint|no-positive-tabindex|2|2|2|2|fc0a48a2c236b462e7fe5ce61af2a0ca6c0bdd55|1695686400000|||addon/templates/components/polaris-date-picker/day.hbs
add|ember-template-lint|no-invalid-interactive|1|31|1|31|a4df5ba56d33be2595fe0bdc113132ad98e56881|1695686400000|||addon/templates/components/polaris-resource-list/checkable-button.hbs
remove|ember-template-lint|no-with|2|2|2|2|f896861689e38028e61f55a0b59fe9146cd8172e|1694649600000|||addon/templates/components/polaris-resource-list.hbs
remove|ember-template-lint|no-curly-component-invocation|6|2|6|2|bbeaac1c9858db996b377c4171d7ed70c5b96347|1702598400000|||addon/templates/components/render-content.hbs
remove|ember-template-lint|no-implicit-this|4|14|4|14|040f06fd774092478d450774f5ba30c5da78acc8|1702598400000|||addon/templates/components/render-content.hbs
add|ember-template-lint|no-yield-only|1|0|1|0|a5fa6e8c1e0f03fb31b5cd17770e4368d44932e4|1704931200000|||addon/components/polaris-resource-list/context/templates/context-provider.hbs
2 changes: 1 addition & 1 deletion addon/components/polaris-resource-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { assert } from '@ember/debug';
import { tagName, layout as templateLayout } from '@ember-decorators/component';
import ContextBoundEventListenersMixin from 'ember-lifeline/mixins/dom';
import ContextBoundTasksMixin from 'ember-lifeline/mixins/run';
import createContext from 'ember-context';
import createContext from './polaris-resource-list/context/context';
import layout from '../templates/components/polaris-resource-list';
import { computedIdVariation } from '@smile-io/ember-smile-polaris/utils/id';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@ember/component';
import layout from '../templates/context-consumer';

/**
* Base consumer component.
*/
export default Component.extend({
tagName: '',

layout,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Component from '@ember/component';
import layout from '../templates/context-provider';

/**
* Base consumer component.
*/
export default Component.extend({
tagName: '',

layout,

/**
* The current value of the context.
*
* @property value
* @type {any}
* @public
*/
value: null,

/**
* Unique provider ID, used to select the
* appropriate provider for a given consumer.
*
* @property _providerId
* @type {String}
* @private
*/
_providerId: null,
});
70 changes: 70 additions & 0 deletions addon/components/polaris-resource-list/context/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Mixin from '@ember/object/mixin';
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
import ProviderComponent from './components/context-provider';
import ConsumerComponent from './components/context-consumer';

/**
* Traverses the provided view's ancestor tree
* to find the nearest provider component of the
* appropriate type.
*/
function findProviderAncestorView(view, providerId) {
let ancestorView = view.get('parentView');

while (ancestorView) {
// Check if ancestor view is context provider.
if (ancestorView.get('_providerId') === providerId) {
return ancestorView;
}

ancestorView = ancestorView.get('parentView');
}

return null;
}

/**
* React-style createContext function.
* Takes a default context value and returns a tuple of
* Provider and Consumer components, and a ConsumerMixin
* used for emulation of React's `Class.contextType`.
*/
export default function createContext(defaultValue) {
const id = new Date().getTime();
const providerId = `provider${id}`;

const Provider = ProviderComponent.extend({
_providerId: providerId,
_value: null,
get value() {
return this._value || defaultValue;
},
set value(val) {
this._value = val;
},
});

// eslint-disable-next-line ember/no-new-mixins
const ConsumerMixin = Mixin.create({
_providerInstance: computed(function () {
return findProviderAncestorView(this, providerId);
}).readOnly(),

context: computed('_providerInstance.value', function () {
const providerInstance = this.get('_providerInstance');

assert('Context consumer missing provider instance', !!providerInstance);

return providerInstance && providerInstance.get('value');
}).readOnly(),
});

const Consumer = ConsumerComponent.extend(ConsumerMixin);

return {
Provider,
Consumer,
ConsumerMixin,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{yield this.context}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{yield}}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"@ember-decorators/component": "^6.0.1",
"@ember/render-modifiers": "^2.1.0",
"@ember/string": "^3.1.1",
"@marianoggf/ember-context": "marianoggf/ember-context#9ea86a4141cd2ee9bd06b7a3dc70eb6abd7ad820",
"@shopify/javascript-utilities": "^2.4.0",
"@shopify/polaris": "3.10.0",
"@shopify/polaris-tokens": "^1.3.1",
Expand Down
18 changes: 0 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading