-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ANCHOR SCROLL: chore(docs): add directive to pad heading anchors #9368
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"browser": true, | ||
"globals": { | ||
"angular": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
angular.module('heading-offset', []) | ||
|
||
/** | ||
* @ngdoc service | ||
* @description | ||
* The offset to use for heading anchors if not specific offset is given using ngOffSet. | ||
* You can set this in a `run` block or update it dynamically based on changing sizes of | ||
* static header. | ||
*/ | ||
.value('headingOffset', {value: '0px' }) | ||
|
||
/** | ||
* @ngdoc directive | ||
* @description | ||
* A directive that matches id attributes on headings (h1, h2, etc) and anchors (a). | ||
* When matched this directive injects a new element that acts as a buffer to ensure that | ||
* when we navigate to the element by id (using a hash on the url) the element appears far | ||
* enough down the page | ||
* | ||
* You can specify the offset on an element by element basis using the `ng-offset` attribute. | ||
* The attribute can be a static value: | ||
* | ||
* ```html | ||
* <h3 id="some-heading" ng-offset="56px">Some Heading</h3> | ||
* ``` | ||
* | ||
* or it can be interpolated: | ||
* | ||
* ```html | ||
* <h3 id="some-heading" ng-offset="{{ offset }}">Some Heading</h3> | ||
* ``` | ||
* | ||
* If no value is given for `ng-offset` then the directive will use the value given by the | ||
* `headingOffset` service. You can set the value of this at runtime: | ||
* | ||
* ```html | ||
* <h3 id="some-heading">Some Heading</h3> | ||
* ``` | ||
* | ||
* ```js | ||
* appModule.run(['headingOffset', function(headingOffset) { | ||
* // Provide the initial offset for heading anchors | ||
* headingOffset.value = '120px'; | ||
* }]); | ||
* ``` | ||
* | ||
* Be aware that this moves the id to a span below the original element which can play havoc with you | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* CSS if you are relying on ids in your styles (which you shouldn't). | ||
* | ||
*/ | ||
.directive('id', ['headingOffset', '$compile', '$anchorScroll', function(headingOffset, $compile, $anchorScroll) { | ||
|
||
return { | ||
restrict: 'A', | ||
compile: function(element, attrs) { | ||
if ( /^(h\d+)|(a)$/i.test(element[0].nodeName || attrs.ngOffset ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex is wrong. It will match anything either starting with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And shouldn't it be:
Here again, same as below, |
||
|
||
console.log('processing anchor for', element[0].nodeName, attrs.id); | ||
|
||
return function postLink(scope) { | ||
|
||
// Create an anchor for this heading | ||
var anchor = $compile('<span style="display: block"></span>')(scope); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: Why a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just copying the idea from other examples. I am not sure if there is any point... |
||
|
||
// Move the id from the original heading element to the span | ||
anchor.attr('id', attrs.id); | ||
element.removeAttr('id'); | ||
|
||
// Insert this anchor as the first child of the heading | ||
element.prepend(anchor); | ||
|
||
var updateStyle = function(offset) { | ||
anchor.css('margin-top', '-'+offset); | ||
anchor.css('height', offset); | ||
}; | ||
|
||
// Work out whether we are using a specific offset or getting the global default | ||
if ( attrs.ngOffset ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a detail, but theoretically we should check if the E.g.
will end up using Additionally, it might be a good idea to add a check in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My view was that if the ngOffset attribute is ever defined then it should never default to the global headingOffset value. Is that wrong do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to support having ngOffset evaluate to something under some condition, or evaluate to undefined or null if the condition is not met and let the default headingOffset be used. E.g. having the following HTML:
we could account for the following scenario (based on how the docs app is designed): This is indeed an implementation choice (i.e. nothing inherenty wrong if we don't support it) and the added flexibility might not be worth the trouble. Up to you :) |
||
scope.$observe('ngOffset', updateStyle); | ||
} else { | ||
scope.$watch(function() { return headingOffset.value; }, updateStyle); | ||
} | ||
}; | ||
} | ||
} | ||
}; | ||
}]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"browser": true, | ||
"globals": { | ||
"angular": false, | ||
"module": false, | ||
"inject": false, | ||
"describe": false, | ||
"it": false, | ||
"beforeEach": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
describe("heading-offset", function() { | ||
describe("id directive", function() { | ||
|
||
var $compile, $scope; | ||
|
||
beforeEach(module('heading-offset')); | ||
|
||
beforeEach(inject(function($rootScope, _$compile_) { | ||
$scope = $rootScope; | ||
$compile = _$compile_; | ||
})); | ||
|
||
it("should inject a span into headings with ids", function() { | ||
var element = $compile('<h1 id="some-id"></h1>')($scope); | ||
var span = element.children(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is kind of too relaxed, because using Using (I am not saying the directive is not working as expected; it is. But if it weren't (or if is breaks in the future), this test might still succeed.) |
||
expect(span[0].nodeName).toMatch(/span/i); | ||
expect(element.attr('id')).toBeUndefined(); | ||
expect(span.attr('id')).toBe('some-id'); | ||
}); | ||
|
||
it("should inject a span into anchors with ids", function() { | ||
var element = $compile('<a id="some-id"></a>')($scope); | ||
var span = element.children(); | ||
expect(span[0].nodeName).toMatch(/span/i); | ||
expect(element.attr('id')).toBeUndefined(); | ||
expect(span.attr('id')).toBe('some-id'); | ||
}); | ||
|
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngOffset
(with lowercases
).