diff --git a/.lint-todo b/.lint-todo index 757d4fac..361e0845 100644 --- a/.lint-todo +++ b/.lint-todo @@ -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 diff --git a/addon/components/polaris-resource-list.js b/addon/components/polaris-resource-list.js index bf24bab3..beae1cbc 100644 --- a/addon/components/polaris-resource-list.js +++ b/addon/components/polaris-resource-list.js @@ -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'; diff --git a/addon/components/polaris-resource-list/context/components/context-consumer.js b/addon/components/polaris-resource-list/context/components/context-consumer.js new file mode 100644 index 00000000..57043132 --- /dev/null +++ b/addon/components/polaris-resource-list/context/components/context-consumer.js @@ -0,0 +1,11 @@ +import Component from '@ember/component'; +import layout from '../templates/context-consumer'; + +/** + * Base consumer component. + */ +export default Component.extend({ + tagName: '', + + layout, +}); diff --git a/addon/components/polaris-resource-list/context/components/context-provider.js b/addon/components/polaris-resource-list/context/components/context-provider.js new file mode 100644 index 00000000..7048dce5 --- /dev/null +++ b/addon/components/polaris-resource-list/context/components/context-provider.js @@ -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, +}); diff --git a/addon/components/polaris-resource-list/context/context.js b/addon/components/polaris-resource-list/context/context.js new file mode 100644 index 00000000..dc18a326 --- /dev/null +++ b/addon/components/polaris-resource-list/context/context.js @@ -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, + }; +} diff --git a/addon/components/polaris-resource-list/context/templates/context-consumer.hbs b/addon/components/polaris-resource-list/context/templates/context-consumer.hbs new file mode 100644 index 00000000..2c935ca5 --- /dev/null +++ b/addon/components/polaris-resource-list/context/templates/context-consumer.hbs @@ -0,0 +1 @@ +{{yield this.context}} \ No newline at end of file diff --git a/addon/components/polaris-resource-list/context/templates/context-provider.hbs b/addon/components/polaris-resource-list/context/templates/context-provider.hbs new file mode 100644 index 00000000..fb5c4b15 --- /dev/null +++ b/addon/components/polaris-resource-list/context/templates/context-provider.hbs @@ -0,0 +1 @@ +{{yield}} \ No newline at end of file diff --git a/package.json b/package.json index f360a506..c6629746 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f2d1a7ad..995d284f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,9 +17,6 @@ dependencies: "@ember/string": specifier: ^3.1.1 version: 3.1.1 - "@marianoggf/ember-context": - specifier: marianoggf/ember-context#9ea86a4141cd2ee9bd06b7a3dc70eb6abd7ad820 - version: github.com/marianoggf/ember-context/9ea86a4141cd2ee9bd06b7a3dc70eb6abd7ad820 "@shopify/javascript-utilities": specifier: ^2.4.0 version: 2.4.1 @@ -18205,18 +18202,3 @@ packages: } engines: { node: ">=12.20" } dev: true - - github.com/marianoggf/ember-context/9ea86a4141cd2ee9bd06b7a3dc70eb6abd7ad820: - resolution: - { - tarball: https://codeload.github.com/marianoggf/ember-context/tar.gz/9ea86a4141cd2ee9bd06b7a3dc70eb6abd7ad820, - } - name: ember-context - version: 0.1.0 - engines: { node: 6.* || 8.* || >= 10.* } - dependencies: - ember-cli-babel: 7.26.11 - ember-cli-htmlbars: 6.3.0 - transitivePeerDependencies: - - supports-color - dev: false